I'm using ByteBuffers
and FileChannels
to write binary data to a file. When doing that for big files or successively for multiple files, I get an OutOfMemoryError
exception.
I've read elsewhere that using Bytebuffers
with NIO is broken and should be avoided. Does any of you already faced this kind of problem and found a solution to efficiently save large amounts of binary data in a file in java?
Is the jvm option -XX:MaxDirectMemorySize
the way to go?
I would say don't create a huge ByteBuffer that contains ALL of the data at once. Create a much smaller ByteBuffer, fill it with data, then write this data to the FileChannel. Then reset the ByteBuffer and continue until all the data is written.