I am new to Java and have been trying to serialize objects to a file using ObjectInputStream and ObjectOutputStream. As a requirement, I am adding image files about 1MB as byte[] since an Image is not serializable. Once I deserialize the byte[], I convert it to BufferedImage so that I can process it such as subimages and so forth. I am attempting to serialize many objects into one file. (greater than 2 GB)
The issues that I am running into is that when I get to 1024MB, I get a OutOfMemory exception. I got the exception earlier and I increased the heap sisze -Xmx1024m.
I know this is not the way to go because I will always run into the OutOf Memory exception.
Now with Java NIO, I am thinking that I can use a memory mapped file using Channels and MappedByteBuffer.
The question is:
How do I serialize my objects into a file using this method to perform a write and a read. I am assuming that I would have to use the ObjectInputStream and ObjectOutputStream with the Channels and ByteBuffer but not sure where to begin.
I would like to use the Java NIO because it utilizes the Operating System for I/O rather than filling up my JVM Heap which results in OutOfMemory.
Could someone please give me guidance into how I can go about doing this?
I exhaustively search various forums and Googled it and could not find a soultion that makes sense to me.