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();
I have no idea about the Android implementation, but in standard Java, good old-fashioned IO often outperforms NIO.
For example I believe the following code should be relatively fast if you have an array of bytes:
Bear in mind that this will block until the entire array of bytes is written. But you don’t say whether non-blocking behaviour is a problem or not.
Another thing you don’t mention is how you intend to encode the integers when writing into the file. You need to perform the encoding in memory before writing to file, but it’s possible that the array is too large to encode all at once, in which case you can encode/write in blocks of several hundred K.