I am trying to write a text compression program in java with huffman algorithm.I have already create the code for the encoding and i run my program from cmd like this: java Main inputFile outputFile where input is a txt file which contains 4 lines :
Hello
My name is
Panagiotis123
Nice to meet you!
I read line by line the txt and i want to create an compressed output file which will contains the text encoded:
111000110110100000100011011110001110100110100010100001000111111110011110100101010001011110111111101110101111110011101110011000111001001111001011000111010101101100001001001010011110101011110000111001
the input file contains 45 characters and the output file will contain 195 BITS so the first file will be approximately 45 bytes and the outputfile 195/8... i have try this :
int b;
while ((line2 = br2.readLine()) != null) {
String a = Encode.encode(line2, hTree);
for(int k = 0; k < a.length(); k++) {
b = a.charAt(k);
fos.write((byte)b);
}
}
where a is a String which contains the encoded line. fos is created like this
FileOutputStream fos = new FileOutputStream(new File(args[1]));
The input file eventually is 54 bytes and the outputFile 198... Obviously it takes the 195 0/1 as characters and not as bit...
- what can i do?
- the String a how long can it be? i mean if there is a line with 8 words the encoded text may be more than 120 there would be a problem if i assign so many bits?