サイズ 400000000 の大きな Long Array をファイルに書き込んで読み込もうとしています。私が使用しているコードは次のとおりです。
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 < 400000000 ; i++){
k[i] = i ;
}
channel = new RandomAccessFile("abc.dat", "rw").getChannel();
mbb = channel.map(FileChannel.MapMode.READ_WRITE, 0, 1 << 24);
mbb.order(ByteOrder.nativeOrder());
for(int i = 0 ; i < 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 << 24);
mbb.order(ByteOrder.nativeOrder());
for(int r = 0 ; r < 400000000; r++){
ks[r] = getMbb().getLong();
}
for(int r = 0 ; r < 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() <= 0) {
mbb = channel.map(FileChannel.MapMode.READ_WRITE, channel.size(), 1 << 24);
mbb.order(ByteOrder.nativeOrder());
}
return mbb;
}
}
ただし、このコードは、書き込み配列と読み取り配列が同じではないというエラーを出しています。なぜこれが起こっているのですか?