I am getting IOException: Map Failed when trying to write a large byte array. I use the method below to write a byte array to a file
private static void write(byte[] data) throws Exception {
File file = new File("C:/temp/file.json");
int length = data.length;
RandomAccessFile raf = new RandomAccessFile(file, "rw");
FileChannel fc = raf.getChannel();
MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_WRITE, 0, length);
for (int i = 0; i < length; i++) {
buffer.put(data[i]);
}
}
The byte array is about 270mb.
Can anyone explain what I am doing wrong?
Thanks.
I am not sure why the map failed, but I wouldn’t do it the way you have done.
to do it progressively you can use
The map could fail if you have a 32-bit JVM and you call this method repeatedly. e.g. you run out of virtual memory.