Java - Reading Writing Large Long Array Code
<p>I am trying to write a large Long Array of size 400000000 into a file and then read it back. What code I am using is following:</p>

<pre><code>import java.io.*;
import java.nio.*;
import java.nio.channels.FileChannel;
import java.io.RandomAccessFile ;
import java.util.* ;

class Checks {
public static FileChannel channel;
public static MappedByteBuffer mbb;

public static void main(String[] args){
    try{


      long k[] = new long[400000000] ;
          for(int i = 0 ; i &lt; 400000000 ; i++){
            k[i] = i ;
          }

    channel = new RandomAccessFile("abc.dat", "rw").getChannel();
        mbb = channel.map(FileChannel.MapMode.READ_WRITE, 0, 1 &lt;&lt; 24);
        mbb.order(ByteOrder.nativeOrder());

          for(int i = 0 ; i &lt; 400000000 ;i++ ){
             getMbb().putLong(k[i]);
        }

        channel.close();

       long ks[] = new long[400000000] ;

        channel = new RandomAccessFile("abc.dat", "rw").getChannel();
        mbb = channel.map(FileChannel.MapMode.READ_WRITE, 0, 1 &lt;&lt; 24);
        mbb.order(ByteOrder.nativeOrder());

        for(int r = 0 ; r &lt; 400000000; r++){
            ks[r] = getMbb().getLong();
         }

             for(int r = 0 ; r &lt; 400000000; r++){
                 if(k[r] != ks[r]){
                  System.out.println("Error at " + r);
                  break ;
                  }
              }


}
    catch(Exception e)
    {
        e.printStackTrace();
    }

   }

    public static ByteBuffer getMbb() throws IOException {
        if (mbb.remaining() &lt;= 0) {
            mbb = channel.map(FileChannel.MapMode.READ_WRITE, channel.size(), 1 &lt;&lt; 24);
            mbb.order(ByteOrder.nativeOrder());
        }
        return mbb;
    }
}
</code></pre>

<p>However, this code is giving error that write and read array are not same. Can anybody help me why is this happening ?</p>
