I am trying to read the contents of file and then write them to a different file with Huffman coding. So I have created a Huffman Tree, with each node containing the character, frequency of the character, and a binary string representing the frequency. What I am having trouble understanding is writing the Huffman coded characters to a file.
I tried writing the binary string to the file but realized that it was just writing string and not actually the coded data. So I then converted the binary strings to bytes and wrote the bytes to the file but that would just give me a blank file the same size as the original. I feel like I am missing something when it comes to actually writing the file.
Edit: After taking a look back at my code I realized that my Tree wasn't completely correct and I'm now able to (I think) combine bit string together to make a byte array that I can write to a file (code updated to reflect that). For my test case I am reading in the text AAA_BB_C
but when I look at the file the output is <0x1e>
. I'm not sure what this means. I was expecting the same output of the original file, just a smaller size.
public static void writeFile(HuffTree tree) {
String bin = ""; // String of entire binary code
int spot = 0; // Spot in array
byte[] bytes = new byte[256]; // byte array
try {
FloatileWriter writer = new FileWriter("test(encoded).txt");
// Gets Binary String of each Character in the file
for(int i = 0; i < fileText.length(); i++) {
bin += tree.findDataBinary(fileText.charAt(i));
}
// Takes each bit and adds to byte array
System.out.println(bin);
while(bin.length() > 7) {
String temp = bin.substring(0, 7);
bin = bin.substring(7, bin.length());
bytes[spot] = Byte.parseByte(temp, 2);
spot++;
}
// Writes bytes to file
for(int i = 0; i <= spot; i++) {
writer.write(bytes[i]);
}
writer.close();
} catch(IOException e) {
System.out.println("IOException!");
}
}