title: Java-How to write bits and not characters in a file
body: <p>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 :</p>
  
  <pre><code>Hello
  My name is
  Panagiotis123
  Nice to meet you!
  </code></pre>
  
  <p>I read line by line the txt and i want to create an compressed output file which will contains the text encoded:</p>
  
  <blockquote>
    <p>111000110110100000100011011110001110100110100010100001000111111110011110100101010001011110111111101110101111110011101110011000111001001111001011000111010101101100001001001010011110101011110000111001</p>
  </blockquote>
  
  <p>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  :</p>
  
  <pre><code> int b; 
   while ((line2 = br2.readLine()) != null) {
       String a = Encode.encode(line2, hTree);
       for(int k = 0; k &lt; a.length(); k++) {
           b = a.charAt(k);
           fos.write((byte)b);
       }
   }
  </code></pre>
  
  <p>where a is a String which contains the encoded line.
  fos is created like this</p>
  
  <pre><code>FileOutputStream fos = new FileOutputStream(new File(args[1]));
  </code></pre>
  
  <p>The input file eventually is 54 bytes and the outputFile 198...
  Obviously it takes the 195 0/1 as characters and not as bit... </p>
  
  <ol>
  <li>what can i do?</li>
  <li>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?</li>
  </ol>