I need to read a large text file of around 5-6 GB line by line using Java.
How can I do this quickly?
In Java 8, there is also an alternative to using Files.lines(). If your input source isn't a file but something more abstract like a Reader
or an InputStream
, you can stream the lines via the BufferedReader
s lines()
method.
For example:
try (BufferedReader reader = new BufferedReader(...)) {
reader.lines().forEach(line -> processLine(line));
}
will call processLine()
for each input line read by the BufferedReader
.