title: Fastest way to write an array of integers to a file in Java?
body: <p>As the title says, I'm looking for the fastest possible way to write integer arrays to files. The arrays will vary in size, and will realistically contain anywhere between 2500 and 25 000 000 ints.</p>
  
  <p>Here's the code I'm presently using:</p>
  
  <pre><code>DataOutputStream writer = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename)));
  
  for (int d : data)
    writer.writeInt(d);
  </code></pre>
  
  <p>Given that DataOutputStream has a method for writing arrays of bytes, I've tried converting the int array to a byte array like this:</p>
  
  <pre><code>private static byte[] integersToBytes(int[] values) throws IOException {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      DataOutputStream dos = new DataOutputStream(baos);
      for (int i = 0; i &lt; values.length; ++i) {
          dos.writeInt(values[i]);
      }
  
      return baos.toByteArray();
  }
  </code></pre>
  
  <p>and like this:</p>
  
  <pre><code>private static byte[] integersToBytes2(int[] src) {
      int srcLength = src.length;
      byte[] dst = new byte[srcLength &lt;&lt; 2];
  
      for (int i = 0; i &lt; srcLength; i++) {
          int x = src[i];
          int j = i &lt;&lt; 2;
          dst[j++] = (byte) ((x &gt;&gt;&gt; 0) &amp; 0xff);
          dst[j++] = (byte) ((x &gt;&gt;&gt; 8) &amp; 0xff);
          dst[j++] = (byte) ((x &gt;&gt;&gt; 16) &amp; 0xff);
          dst[j++] = (byte) ((x &gt;&gt;&gt; 24) &amp; 0xff);
      }
      return dst;
  }
  </code></pre>
  
  <p>Both seem to give a minor speed increase, about 5%. I've not tested them rigorously enough to confirm that.</p>
  
  <p>Are there any techniques that will speed up this file write operation, or relevant guides to best practice for Java IO write performance?</p>