Hey guys, I'm working on a project to use Huffman trees to compress a text file into binary. For instance, based on the frequency of the characters in a string like "aardvark",

a - 3
r - 2
d - 1
v - 1
k - 1,

compression using a Huffman tree could look something like 001011011100101111, assuming that the most frequent character (a) would be stored using the least number of bits (0), and one of the least frequently occurring characters (k) would be stored with the most number of bits (1111), and so forth. I am able to form the tree and get the encoded sequence well, but I would now like a way to write each character as one bit rather than the usual full byte used to write a char to a file. I've tried using FileWriter and OutputStream classes such as ByteArrayOutputStream and DataOutputStream, but all seem to use 8 bits per character. I've tried searching quite a bit through the API and on Google but have seen nothing so far that seems effective. Does anyone know of a class or technique that would allow this compressed writing? Thanks in advance.

Have a look at BitSet. It holds long strings of bits, fully packed at 64 bits per 8 bytes, and can be written/read to/from files as a single Object using Object input/output streams.

This question has already been answered. Start a new discussion instead.