954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Writing bits to file

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.

pi_lord12
Light Poster
31 posts since Jun 2010
Reputation Points: 10
Solved Threads: 1
 

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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Yep, that did the trick. Thanks!

pi_lord12
Light Poster
31 posts since Jun 2010
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: