alessandro
alessandro

Reputation: 1721

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

I have an array of 1.5 billion long entry. Now, I want to write in into disk and read it back again. Can anybody help me is there any java library (or custom procedure) to do it efficiently.

Usually, I do it using FileChannel and MappedByteBuffer. But, for 1.5 billion long entry it 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());

Upvotes: 4

Views: 1738

Answers (1)

Jua
Jua

Reputation: 26

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

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

Then , when you need to store on disk your array, 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 to retrieve

FileInputStream infile = new 
    FileInputStream("pathtofile");

ObjectInputStream inobj = 
    new ObjectInputStream (infile);

Object obj = inobj.readObject();

MyWrapper myWrapperInstance = (MyWrapper) obj;

Upvotes: 1

Related Questions