Re: How to (efficiently) write an int array into a file ?
Alexandre Ferrieux wrote:
How do we accomplish this simple task of a direct write to disk of a
big (packed) array of ints, in native byte order ?
What Patricia said. Tested, but not profiled or anything:
package fubar;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;
public class DirectWrite {
private static final int CAP = 8*1024;
private static final int[] testArray =
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
public static void main( String[] args ) throws
FileNotFoundException, IOException
{
File f = new File("test.test");
FileOutputStream fos = new FileOutputStream( f );
ByteBuffer bb = ByteBuffer.allocateDirect( CAP );
IntBuffer ib = bb.asIntBuffer();
ib.put( testArray );
FileChannel fc = fos.getChannel();
fc.write( bb );
fc.close();
// tidy up
f.delete();
}
}