<p>If you have binary strings (literally String objects that contain only 1's and 0's), how would you output them as bits into a file?</p>

<p>This is for a text compressor I was working on; it's still bugging me, and it'd be nice to finally get it working. Thanks!</p>

Easiest is to simply take 8 consecutive characters, turn them into a byte and output that byte. Pad with zeros at the end if you can recognize the end-of-stream, or add a header with length (in bits) at the beginning of the file.
The inner loop would look something like:
```
```
byte[] buffer = new byte[ ( string.length + 7 ) / 8 ];
for ( int i = 0; i < buffer.length; ++i ) {
   byte current = 0;
   for ( int j = 7; j >= 0; --j )
       if ( string[ i * 8 + j ] == '1' )
           current |= 1 << j;
   output( current );
}
```
```
You'll need to make some adjustments, but that's the general idea.