Is there a way in Java to write to disk a large array of, say, integers? I am doing this on an Android, and have not found a method that comes anywhere close to native C code.
The resulting file need not be portable to different machines with different representations, so logically just a bulk write of the underlying bytes should be sufficient. But I don't know how to do that efficiently from Java.
I have tried searching the net, and tested the following:
- Serialization - very slow, as expected.
- Using NIO - still slow - Android trace reveals operations one at a time per integer:
Thanks in advance
NIO code:
int[] array = new array[10000000];
...
raf = new RandomAccessFile(ti.testFileName, "rw");
chan = raf.getChannel();
MappedByteBuffer out = chan.map(FileChannel.MapMode.READ_WRITE, 0, array.length*4);
ib = out.asIntBuffer();
ib.put(array);
out.force();
raf.close();