I have written an algorithm to implement Huffman Coding for compressing text files. It basically takes in a string as an input and generates a string of bits as output. However, I am having trouble storing this binary data as it is being stored as a string where each bit is a character and consumes 2 bytes of memory for storage. End result, output file is larger than the input, making the whole program worthless. How should I store this binary output such that each bit takes only one bit of memory for storage? ps. I have tried using a BitSet but that did not change the size of the output at all
Asked
Active
Viewed 2,532 times
5
-
2Store your data in a byte array. – Stanislav Mamontov Oct 21 '13 at 18:52
-
1As the comment above suggest, use a byte[] which you can write to a File with the FileOutputStream – Philipp Seeger Oct 21 '13 at 18:54
-
tried it, didn't work. I guess its because each bit is stored as a byte but I might be wrong. The goal is to make the output much smaller than the input – Prathamesh Oct 21 '13 at 18:56
-
The problem is the intermediate step, don't store them in a string at all. Just dump them into a byte[] (or BitSet) – arynaq Oct 21 '13 at 18:57
1 Answers
1
Once you have your result in the BitSet
, you can call
BitSet.toByteArray()
to save your data to a file, i.e.:
FileUtils.writeByteArrayToFile(new File(...), bitSet.toByteArray());
And BitSet.valueOf(byte[])
to read your data from file:
BitSet bitSet = new BitSet(FileUtils.readFileToByteArray(new File(...)));
![](../../users/profiles/1851024.webp)
Andrey Chaschev
- 15,055
- 5
- 43
- 63