I want to read a large file in a very fast way. I am using MappedByteBuffer like this:
String line = "";
try (RandomAccessFile file2 = new RandomAccessFile(new File(filename), "r"))
{
FileChannel fileChannel = file2.getChannel();
MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
for (int i = 0; i < buffer.limit(); i++)
{
char a = (char) buffer.get();
if (a == '\n'){
System.out.println(line);
line = "";
}else{
line += Character.toString(c);
}
}
This is not working correctly. It is changing the content of the file and printing the changed content. Is there a better way to read a line of a file with MappedByteBuffer?
Eventually I would like to split the line and extract certain content (since its csv) so this is just a minimal example that reproduces the problem.
I made some tests using a 21 GB file filled with random strings, each line had a length of 20-40 characters. It seems like the builtin BufferedReader is still the fastest method.
Reading the lines to a stream ensures you read the lines as you need them instead of reading the entire file at once.
To improve speed even further you can increase the buffer size of the BufferedReader by a moderate factor. In my tests it starter to outperform the normal buffer size at about 10 millions lines.
The code I used for testing:
Btw I noticed that FileChannel.map throws an exception if the mapped file is larger than Integer.MAX_VALUE which makes the method impractical for reading very large files.