How to Read a Large Text File Line by Line in Java
-
BufferedReader
to Read File Line by Line in Java -
Stream
to Read File Line by Line in Java -
Scanner
to Read File Line by Line in Java

This tutorial discusses methods to read a large text file line by line efficiently in Java.
There are numerous methods to read a text file in Java. However, this tutorial is specific to reading large text files, and we will discuss the three most efficient methods to read large text files quickly.
BufferedReader
to Read File Line by Line in Java
BufferedReader
class in Java reads text from a given character-input stream, buffering characters to provide for the efficient reading of characters, arrays, and lines. This method provides an efficient line by line reading for the input files with a considerably large file size.
The below example illustrates the use of BufferedReader
to read a txt
file and output its content line by line.
import java.io.*;
public class Main {
public static void main(String[] args) {
String file = "my-file.txt";
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
We discussed three methods to read a large text file in Java and process it line by line. Each method had some constraints and advantages that we must consider while deciding which method to use in a particular scenario.