title: How do i do external merge sort on binary file
body: <p>This short program makes a binary file called data.bin with a bunch of randomly generated items that are 1024 bytes each. where the first 24 bytes of each item is the key. so how do i read each item from the data.bin file and do an external merge sort on it all?</p>
  
  <pre><code>import java.math.*;
  import java.io.*;
  import java.util.*;
  
  public class CreateRandomDataFile {
  
     public static void main(String[] args) throws Exception {
  
        RandomAccessFile data = new RandomAccessFile("data.bin","rws");
        Random r = new Random(482010);
        Scanner input = new Scanner(System.in);
        int number = 100000;
        byte[] item = new byte[1024];
  
        System.out.print("How many items (perhaps 800000)\n&gt; ");
        number = input.nextInt();
  
        for (int i=0; i&lt;number; i++) {
           r.nextBytes(item);
           data.write(item);
        }
        data.close();
          System.out.println("Done");
     }
  }  
  </code></pre>