title: Convert image and audio files to binary in Java
body: <p>I want to convert an image and an audio file into a binary stream, process it and then reconstruct the image from the same binary stream in Java. How can I do that? Has anybody worked on this? Please help me out as soon as possible. Any hints or pseudocode will be highly appreciated.
  This is how I tried to do it but it just creates an empty file while reconstructing the image.
  For image to binary:-</p>
  
  <pre><code>        File file = new File("E:\\image.jpg");
          BufferedImage img = ImageIO.read(file);
  
          // write image to byte array in-memory (jpg format)
          ByteArrayOutputStream b = new ByteArrayOutputStream();
          ImageIO.write(img, "jpg", b);
  
          byte[] jpgByteArray = b.toByteArray();
  
          // convert it to a String with 0s and 1s
          StringBuilder sb = new StringBuilder();
          for (byte by : jpgByteArray) {
              sb.append(Integer.toBinaryString(by &amp; 0xFF));
  </code></pre>
  
  <p>For binary to image:-</p>
  
  <pre><code>        byte[] original = obj.orig_seq.getBytes();
  
          InputStream in = new ByteArrayInputStream(original);
          BufferedImage img = ImageIO.read(in);
  
          ImageIO.write(img, "jpg",
                   new File("E:\\mypic_new.jpg"));
  </code></pre>