title: What is the best way to write an int array (image data) to file
body: <p>I have a large int array containing image data in the format ARGB (alpha, R, G and B channels, 1 byte each). I want to save it to file in <code>onPause()</code> to be able to reload it when the app is restarted. What do you think is the best way to do that?</p>
  
  <p>I found the following methods:</p>
  
  <ul>
  <li><p>Convert the int array to a byte array manually (see <a href="http://www.java2s.com/Code/Java/Collections-Data-Structure/intarraytobytearray.htm" rel="nofollow">here</a>) and then use a <code>FileOutputStream</code> to output the byte array.</p></li>
  <li><p>Wrap the array into a <code>java.nio.IntBuffer</code> and then write the object to file using <code>java.io.ObjectOutputStream.writeObject()</code>.</p></li>
  <li><p>Write each element one at a time using <code>java.io.ObjectOutputStream.writeInt()</code>.</p></li>
  </ul>
  
  <p>All these methods seem quite wasteful so there is probably another, better way. Possibly even a way to use image compression to reduce the size of the file?</p>