title: How to output binary data to a file in Java?
body: <p>I'm trying to write data to a file in binary format for compression. The data consists entirely of floating points so I decided to quantize the data to an intergers between 0 and 65535 so the data can be written as two bit unsigned integers and ultimately save space. However, I need to output that quantized data to a file in binary instead of human-readable Ascii.</p>
  
  <p>At the moment this is what I'm doing</p>
  
  <p>@param outputFile the file containing the already quantized data as strings in a .txt file<br /></p>
  
  <pre><code>public void generateBinaryRioFile(String materialLibrary,
          String outputFile, String group, String mtlAux) {
  
      try {
  
          // Create file
          FileWriter fileStream = new FileWriter(outputFile);
          try {
  
              BufferedReader br = new BufferedReader(new FileReader(new File(
                      "idx.txt")));
  
              while ((line = br.readLine()) != null) {
                  writer.write(line + "\n");
              }
              try {
                  br.close();
  
              } catch (FileNotFoundException e) {
                  e.getMessage();
              } catch (IOException e) {
                  e.printStackTrace();
              }           BufferedWriter writer = new BufferedWriter(fileStream);
  </code></pre>
  
  <p>However that writes to the file as a human readable string. I need it to be written as binary data. How does one go about doing this in Java?</p>