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, you could do:
try (Stream lines = Files.lines (file, StandardCharsets.UTF_8))
{
for (String line : (Iterable) lines::iterator)
{
;
}
}
Some notes: The stream returned by Files.lines
(unlike most streams) needs to be closed. For the reasons mentioned here I avoid using forEach()
. The strange code (Iterable
casts a Stream to an Iterable.