我已经创建了一个霍夫曼编码算法,然后我在String
中编写了二进制代码,并使用FileOutputStream
和DataOutputStream
将其放在二进制文件中。
但现在我无法理解如何阅读它?我需要从二进制文件中获取1和0序列。
.readString()
DataInputStream
的方法
try{
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Binary", "*.bin"));
FileOutputStream fileOutputStream = new FileOutputStream(fileChooser.showSaveDialog(window));
DataOutputStream outputStream = new DataOutputStream(fileOutputStream);
outputStream.writeChars(Main.string_ready_encode);
}catch (IOException e){
e.printStackTrace();
}
Main.string_ready_encode
包含就绪序列
答案 0 :(得分:0)
您的编写代码的问题是您没有指定文件格式。我们现在只能知道它有多少字节才能读取文件。如果您确实知道这一点,可以通过执行以下操作来阅读它:
#ifndef HASH_H
#define HASH_H
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <cmath>
#include <list>
#include <sstream>
#include <locale>
#include <typeinfo>
#include <algorithm>
#include <numeric>
using namespace std;
/*HashTable Node Class*/
template <typename K, typename V>
class HashNode{
private:
public:
HashNode(K key, V value);
HashNode* next;
list<K> keys;
list<V> values;
K key;
V value;
};
//Create node and insert key/value value
template <typename K, typename V>
HashNode<K,V>::HashNode(K key,V value){
// this->value = value;
// this->key = key;
//this->next = NULL;
list<K> keys;
list<V> values;
}
/*HashTable Class*/
template <typename K, typename V>
class hashtable{
private:
HashNode<K,V>** array;
int capacity;
int size;
public:
hashtable();
hashtable(int input);
hashtable(const hashtable& input);
int hashify(int key);
int hashify(string key);
void insert(K key, V value);
void clear(int key);
bool Search(int key);
bool Search(string key);
int getsize();
void print();
void resize();
};
//Hashtable Constructor
template <typename K, typename V>
hashtable<K,V>::hashtable(){
capacity = 413158511;
array = new HashNode<K,V>* [413158511];
for(int i=0;i<capacity;i++)
array[i] = NULL;
//16837687
//20011603
//24092093
//30020381
//104395301
}
//Hashtable Constructor
template <typename K, typename V>
hashtable<K,V>::hashtable(int input){
capacity = input;
array = new HashNode<K,V>* [input];
for(int i=0;i<capacity;i++)
array[i] = NULL;
//16837687
//20011603
//24092093
//30020381
//104395301
}
//HashTable Copy Constructor
template <typename K, typename V>
hashtable<K,V>::hashtable(const hashtable& input){
capacity = input.capacity;
size = input.size;
array = new HashNode<K,V>* [capacity];
HashNode<K,V>* temp;
HashNode<K,V>* entry;
HashNode<K,V>* entry2;
for(int i=0;i<capacity;i++){
if(input.array[i] != NULL){
temp = new HashNode<K,V>(input.array[i]->key,input.array[i]->value);
array[i] = temp;
entry = input.array[i];
entry2 = array[i];
while(entry->next != NULL){
entry = entry->next;
entry2->next = new HashNode<K,V>(entry->key,entry->value);
entry2 = entry2->next;
}
}
}
}
//Resize Function
template <typename K, typename V>
void hashtable<K,V>::resize(){
HashNode<K,V>** array2 = new HashNode<K,V>* [capacity*3];
HashNode<K,V>* temp;
HashNode<K,V>* entry;
HashNode<K,V>* entry2;
for(int i=0;i<capacity;i++){
if(array[i] != NULL){
temp = new HashNode<K,V>(array[i]->key,array[i]->value);
array2[i] = temp;
entry = array[i];
entry2 = array2[i];
while(entry->next != NULL){
entry = entry->next;
entry2->next = new HashNode<K,V>(entry->key,entry->value);
entry2 = entry2->next;
}
}
}
capacity = capacity*3;
array = new HashNode<K,V>* [capacity];
for(int i=0;i<capacity;i++){
if(array2[i] != NULL){
temp = new HashNode<K,V>(array2[i]->key,array2[i]->value);
array[i] = temp;
entry = array2[i];
entry2 = array[i];
while(entry->next != NULL){
entry = entry->next;
entry2->next = new HashNode<K,V>(entry->key,entry->value);
entry2 = entry2->next;
}
}
}
}
//Create index for a key
template <typename K, typename V>
int hashtable<K,V>::hashify(int key){
return key % capacity;
}
//Create index for a key with string
template <typename K, typename V>
int hashtable<K,V>::hashify(string key){
//int temp;
// if( typeid(key).name() == typeid(temp).name() ){
// unsigned int temp = accumulate(key.begin(),key.end(),0);
// temp = temp % capacity;
hash<string> strhash;
return strhash(key)%capacity;
// }
// else
// return key % capacity;
}
//INsert value to the table
template <typename K, typename V>
void hashtable<K,V>::insert(K key, V value){
//if(size == capacity){
// resize();
//}
hash<string> strhash;
int index = strhash(key)%capacity;
if (array[index] == NULL){
array[index] = new HashNode<K,V>(key,value);
array[index]->keys.push_back(key);
array[index]->values.push_back(value);
}
else{
array[index]->keys.push_back(key);
array[index]->values.push_back(value);
}
/* size++;
hash<string> strhash;
int index = strhash(key)%capacity;
if (array[index] == NULL){
array[index] = new HashNode<K,V>(key,value);
}
else {
HashNode<K,V>* entry = array[index];
while (entry->next != NULL)
entry = entry->next;
entry->next = new HashNode<K,V>(key,value);
}*/
}
//Delete a value from the table
template <typename K, typename V>
void hashtable<K,V>::clear(int key){
int index = hashify(key);
// while(array[index] != NULL){
//if(array[index]->key == key){
//HashNode<K,V>* temp = array[index];
//array[index] = dummy;
// array[index]->key = NULL;
//array[index]->value = NULL;
array[index] = NULL;
size--;
//return temp->value;
//}
// index++;
// index %= capacity;
// }
//return NULL;
}
//Find value in the table(int)
template <typename K, typename V>
bool hashtable<K,V>::Search(int key){
int index = hashify(key);
// while(array[index] != NULL){
if(array[index]->key == key)
return true;
//index++;
//index %= capacity;
// }
else
return false;
}
//Find value in the table(string)
template <typename K, typename V>
bool hashtable<K,V>::Search(string key){
/* int index = hashify(key);
// while(array[index] != NULL){
if(array[index] == NULL)
return false;
else if(array[index]->key == key)
return true;
HashNode<K,V>* entry = array[index];
while (entry->next != NULL){
entry = entry->next;
if(entry->key == key)
return true;
}
return false; */
hash<string> strhash;
int index = strhash(key)%capacity;
if(array[index] == NULL)
return false;
// key = final;
//cout << key << end;
//transform(key.begin(), key.end(), key.begin(), ::tolower);
/* wstring word(key.begin(),key.end());
stemming::english_stem<> StemEnglish;
StemEnglish(word);
const std::string s(word.begin(),word.end());*/
list<string>::iterator findIter = find(array[index]->keys.begin(),array[index]->keys.end(),key);
if(findIter != array[index]->keys.end())
return true;
else
return false;
}
//Return the size of the table
template <typename K, typename V>
int hashtable<K,V>::getsize(){
return size;
}
//Print the contents of the table
template <typename K, typename V>
void hashtable<K,V>::print(){
for(int i=0;i<capacity;i++){
if(array[i] != NULL ){
HashNode<K,V>* entry = array[i];
while (entry != NULL){
cout << "Key = " << entry->key << " Value = " << entry->value << endl;
entry = entry->next;
}
}
}
}
#endif // HASH_H
但实际上我建议你用一些已知的文件格式重写你的文件,如下所示:
try (DataInputStream stream = new DataInputStream(new FileInputStream(f))) {
byte[] bytes = new byte[NUMBER_OF_BYTES];
stream.read(bytes);
String content = new String(bytes);
System.out.println(content);
} catch (IOException e) {
e.printStackTrace();
}
阅读它就像你会阅读任何其他文件一样:
try (Writer writer = new OutputStreamWriter(new FileOutputStream(f), Charsets.UTF_8)) {
writer.write(Main.stringReadyEncode, 0, Main.stringReadyEncode.length());
} catch (IOException x) {
e.printStackTrace();
}
请确保在写入文件时使用您使用的任何编码替换try (BufferedReader r = Files.newBufferedReader(f.toPath(), Charsets.UTF_8)) {
String line;
while((line = r.readLine()) != null) {
// do whatever you want with line
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
。