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!");
}
}
fileText? --- What is the point ofmap? ---byteis a primitive and doesn't have any methods, so what did you expecttempByte.array()to do? Create a 1-byte array? --- Your code seems to just map each character with a 1-byte value, so not much data compression going on. Seems you missed the entire point of Huffman. You sure you understand how Huffman works? It works with bits. so you need to do bit-manipulation. - AndreasfindDataBinary()? Presumably it's aString, since that's whatparseByte()requires, but is it a bit-string like"10011"? If so, then callingparseByte()to turn it into the decimal number 10011 seems meaningless. Wouldn't you at the very least want to parse it as binary, e.g. base-2? --- And let's say the first letter maps to111and the second letter maps to10011, that would mean that they combined maps to11110011, which is 8 bits and hence a single byte, compressing 2 letters into 1 byte, but you do no merging of bits. Step back and try again - AndreasfindDataBinary()returns a bit-string of a character. My byte array should also work as well now. But I'm still stuck. - billybob643