I am trying to write a lot of data into a binary file. Because it is a lot of data, it is important that this is done fast and I want to be able to write the data as ints one by one. I have tried RandomAccessFile, BufferedWriter, DataOutputStream etc. but all of those are either too slow or cannot write ints. Any ideas that might help me?
I am trying to write a lot of data into a binary file. Because
Share
Every stream can ‘write ints’ if you write the correct code to convert ints to bytes.
The two ‘fast’ IO options in Java are
BufferedOutputStreamon top ofFileOutputStreamand the use of aFileChannelwith NIO buffers.If all you are writing is many, many,
intvalues, you can useIntBufferinstances to pass the data to afileChannel.Further, ‘one at a time’ is generally incompatible with ‘fast’. Sooner or later, data has to travel to the disk in blocks. If you force data to disk in small quantities, you will find that the process is very slow. You could, for example, add integer values to a buffer and write the buffer when it fills, and then repeat.