A
A
Anton @ Lyalin2017-01-19 11:30:36
Java
Anton @ Lyalin, 2017-01-19 11:30:36

How to write data to a file very quickly??

I write the csv file with data, 1.000.000 lines. For this I use bufferewriter. If you write 1000 lines, it's fast, if 10,000, then it's generally slow. How can you speed up the process?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Vereschaka, 2017-01-19
@toxa_1995

In any case, it will not work quickly, since this is an IO operation on the disk. Much depends on the disk itself.
I recommend trying Memory Mapped Files, but you need to know how much you will write in order to reserve space.

byte[] buffer = new byte[4096];
int numOfLines = 1000000;

FileChannel rwChannel = new RandomAccessFile("textfile.txt", "rw").getChannel();
ByteBuffer wrBuf = rwChannel.map(FileChannel.MapMode.READ_WRITE, 0, buffer.length * numOfLines);
for (int i = 0; i < numOfLines; i++)
{
    wrBuf.put(buffer);
}
rwChannel.close();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question