I need to read a large text file of around 5-6 GB line by line using Java.
How can I do this quickly?
By using the org.apache.commons.io package, it gave more performance, especially in legacy code which uses Java 6 and below.
Java 7 has a better API with fewer exceptions handling and more useful methods:
LineIterator lineIterator = null;
try {
lineIterator = FileUtils.lineIterator(new File("/home/username/m.log"), "windows-1256"); // The second parameter is optionnal
while (lineIterator.hasNext()) {
String currentLine = lineIterator.next();
// Some operation
}
}
finally {
LineIterator.closeQuietly(lineIterator);
}
commons-io
commons-io
2.6