Java How to write / read a very large long array to a file

I have an array of 1.5 billion lengths. Now I want to write to disk and read it again. Can someone help me if there is any java library (or user procedure) to do this efficiently.

I usually use FileChannel and MappedByteBuffer. But, for a 1.5 billion long record, this simply exceeds the limit.

Edit:

FileChannel ch = new RandomAccessFile(path, "r").getChannel(); MappedByteBuffer mb = ch.map(FileChannel.MapMode.READ_ONLY, 0, ch.size()); mb.order(ByteOrder.nativeOrder()); 
+4
source share
1 answer

I have never tried objects of this size, but I think you can try to wrap your array inside a class. java.io.Serializable interface:

  class MyWrapper implements java.io.Serializable { Object[] myArray; } 

Then, when you need to save your array to disk, you will do it simply using the interface method:

 FileOutputStream fouts = new FileOutputStream("pathtofile"); // Write object with ObjectOutputStream ObjectOutputStream outobj= new ObjectOutputStream (fouts); // Write object out to disk outobj.writeObject ( myWrapperInstance ); 

In order of receipt

 FileInputStream infile = new FileInputStream("pathtofile"); ObjectInputStream inobj = new ObjectInputStream (infile); Object obj = inobj.readObject(); MyWrapper myWrapperInstance = (MyWrapper) obj; 
+1
source

All Articles