Java 8+

Use Java NIO.2 features, including the Paths and Files utility classes.

Call Files.lines to get a stream a Stream of the lines read from the file. Defaults to UTF-8 character encoding, with option to specify character-encoding.

String fileName = "path/to/example.txt" ;
try 
    (
        Stream<String> stream = Files.lines(Paths.get(fileName))
    ) 
{
    stream.forEach(System.out::println);
}

You have to place the Stream in a try-with-resource block to ensure the #close method is called on it, otherwise the underlying file handle is never closed until the garbage collector does it much later.

Earlier Java

A common pattern is to use BufferedReader.

try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    String line;
    while ((line = br.readLine()) != null) {
       // process the line.
    }
}

You can read the data faster if you assume there is no character encoding. e.g. ASCII-7 but it won't make much difference. It is highly likely that what you do with the data will take much longer.

EDIT: A less common pattern to use which avoids the scope of line leaking.

try(BufferedReader br = new BufferedReader(new FileReader(file))) {
    for(String line; (line = br.readLine()) != null; ) {
        // process the line.
    }
    // line is not visible here.
}
Answer from Peter Lawrey on Stack Overflow
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ java-read-file-line-by-line
Java Read File: Complete Guide with Examples | DigitalOcean
February 20, 2025 - Continue your learning with the BufferedReader API Doc (Java SE 8). You can use the Scanner class to open a file and then read its content line-by-line.
Top answer
1 of 16
1258

Java 8+

Use Java NIO.2 features, including the Paths and Files utility classes.

Call Files.lines to get a stream a Stream of the lines read from the file. Defaults to UTF-8 character encoding, with option to specify character-encoding.

String fileName = "path/to/example.txt" ;
try 
    (
        Stream<String> stream = Files.lines(Paths.get(fileName))
    ) 
{
    stream.forEach(System.out::println);
}

You have to place the Stream in a try-with-resource block to ensure the #close method is called on it, otherwise the underlying file handle is never closed until the garbage collector does it much later.

Earlier Java

A common pattern is to use BufferedReader.

try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    String line;
    while ((line = br.readLine()) != null) {
       // process the line.
    }
}

You can read the data faster if you assume there is no character encoding. e.g. ASCII-7 but it won't make much difference. It is highly likely that what you do with the data will take much longer.

EDIT: A less common pattern to use which avoids the scope of line leaking.

try(BufferedReader br = new BufferedReader(new FileReader(file))) {
    for(String line; (line = br.readLine()) != null; ) {
        // process the line.
    }
    // line is not visible here.
}
2 of 16
186

Look at this blog:

  • Java Read File Line by Line - Java Tutorial

The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.

// Open the file
FileInputStream fstream = new FileInputStream("textfile.txt");

// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));

String strLine;

//Read File Line By Line
while ((strLine = br.readLine()) != null)   {
  // Print the content on the console
  System.out.println (strLine);
}

//Close the input stream
in.close();
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ reading-a-file-line-by-line-in-java
Reading a File Line by Line in Java
July 24, 2023 - One of the easiest ways of reading a file line by line in Java could be implemented by using the Scanner class.
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ java โ€“ read a text file line by line
Java - Read a text file line by line - Mkyong.com
December 27, 2016 - package com.mkyong; import ... File("src/com/mkyong/data.txt"); System.out.println("Reading files using Apache IO:"); List<String> lines = FileUtils.readLines(f, "UTF-8"); for (String line : lines) { System.out.println(line); } } ...
๐ŸŒ
CodeJava
codejava.net โ€บ java-se โ€บ file-io โ€บ how-to-read-text-file-line-by-line-in-java
How to read text file line by line in Java
July 28, 2019 - BufferedReader lineReader = new BufferedReader(new FileReader(filePath)); String lineText = null; List<String> listLines = new ArrayList<String>(); while ((lineText = lineReader.readLine()) != null) { listLines.add(lineText); } lineReader.close(); Using the LineNumberReader class is similar to the BufferedReader class, for example:
๐ŸŒ
Javatpoint
javatpoint.com โ€บ how-to-read-file-line-by-line-in-java
How to read file line by line in Java - Javatpoint
How to read file line by line in Java with oops, string, exceptions, multithreading, collections, jdbc, rmi, fundamentals, programs, swing, javafx, io streams, networking, sockets, classes, objects etc,
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ fundamentals-of-text-data-manipulation-in-java-1 โ€บ lessons โ€บ reading-text-files-line-by-line-in-java
Reading Text Files Line-by-Line in Java
To read a file line-by-line in Java, you can use the Files.readAllLines method to read the lines into a List<String>, and then iterate over the list using a for-each loop.
๐ŸŒ
RoseIndia
roseindia.net โ€บ java โ€บ beginners โ€บ java-read-file-line-by-line.shtml
Java Read File Line by Line - Java Tutorial
Here is the video tutorial that ... file one line at a time. In the following example code we have used the BufferedReader class which contains a method readLine() for reading one line of data one by one....
Find elsewhere
๐ŸŒ
Techie Delight
techiedelight.com โ€บ home โ€บ java โ€บ read contents of a file in java 11 and above
Read contents of a file in Java 11 and above | Techie Delight
2 weeks ago - Files.readString(Path) method can be used to read all characters from a file into a string. It accepts the path to the source file and returns a string containing the content read from the file.
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ examples โ€บ read-file
Java Program to Read the Content of a File Line by Line
In the above example, we have used the BufferedReader Class to read the file named input.txt. import java.io.File; import java.util.Scanner; class Main { public static void main(String[] args) { try { // create a new file object File file = new File("input.txt"); // create an object of Scanner // associated with the file Scanner sc = new Scanner(file); // read each line from file and print it System.out.println("Reading File Using Scanner:"); while(sc.hasNextLine()) { System.out.println(sc.nextLine()); } // close scanner sc.close(); } catch (Exception e) { e.getStackTrace(); } } }
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ how to read a file in java
How to read a file in Java - Mkyong.com
July 17, 2020 - 1.1 This example uses the Java 8 Files.lines to read the above file into a Stream, and print it line by line.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ examples โ€บ read-the-content-of-a-file-line-by-line
Java Program to Read the Content of a File Line by Line | Vultr Docs
December 17, 2024 - For example, BufferedReader offers straightforward line-by-line reading, Files.lines supports functional-style operations, while Scanner provides parsing capabilities. Choose the approach that best fits the needs of your application based on ...
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_files_read.asp
Java Read Files
Explanation: This program opens the file named filename.txt and reads it line by line using a Scanner. Each line is printed to the console. If the file cannot be found, the program will print "An error occurred." instead.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java io โ€บ how to read a file in java
How to Read a File in Java | Baeldung
January 8, 2024 - Then we can use BufferedReader to read line by line, Scanner to read using different delimiters, StreamTokenizer to read a file into tokens, DataInputStream to read binary data and primitive data types, SequenceInput Stream to link multiple ...
๐ŸŒ
HowToDoInJava
howtodoinjava.com โ€บ home โ€บ java 8 โ€บ java read a file line by line
Java Read a File Line by Line
August 22, 2023 - In this Java tutorial, we will learn to read a text file line-by-line methods such as Streams or FileReader. We will also learn to iterate through lines and filter the file content based on some conditions.
๐ŸŒ
Java With Us
javawithus.com โ€บ home โ€บ en โ€บ read a file in java: the modern way
Read a File in Java: The Modern Way | Java With Us
April 21, 2026 - import java.util.List; List<String> lines = Files.readAllLines(Path.of("data.txt")); for (String line : lines) { System.out.println(line); } Warning: readAllLines loads the entire file into memory.
๐ŸŒ
Attacomsian
attacomsian.com โ€บ blog โ€บ how-to-read-a-file-line-by-line-in-java
How to read a file line by line in Java
February 17, 2020 - The Scanner class presents the simplest way to read a file line by line in Java. We can use Scanner class to open a file and then read its content line by line. A Scanner breaks its input into tokens using a delimiter pattern, which is a new ...
๐ŸŒ
javaspring
javaspring.net โ€บ blog โ€บ read-file-line-by-line-in-java
Reading Files Line by Line in Java โ€” javaspring.net
In this example, we first create a FileReader object to read the file, and then wrap it with a BufferedReader to enable buffering. The readLine() method of BufferedReader reads a line of text from the file until it reaches the end of the file.
๐ŸŒ
Shapehost
shape.host โ€บ home โ€บ resources โ€บ how to efficiently read a file line-by-line in java
How to Efficiently Read a File Line-By-Line in Java - Shapehost
November 29, 2023 - Another approach to reading a file line-by-line in Java is by utilizing the java.util.Scanner class. With Scanner, we can open a file and read its contents line-by-line with ease. Consider the following example program that demonstrates how to read a file line-by-line using Scanner: