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
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();
🌐
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.
🌐
Stack Abuse
stackabuse.com › reading-a-file-line-by-line-in-java
Reading a File Line by Line in Java
July 24, 2023 - The BufferedReader class represents ... is 8192 bytes, but it could be set to a custom size for performance reasons: BufferedReader br = new BufferedReader(new FileReader(file), bufferSize);...
🌐
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.
🌐
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.
🌐
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,
🌐
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.
🌐
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 org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.util.List; public class ReadTextFile { public static void main(String[] args) throws IOException { try { File f = new 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); } } catch (IOException e) { e.printStackTrace(); } } }
Find elsewhere
🌐
Programiz
programiz.com › java-programming › examples › read-file
Java Program to Read the Content of a File Line by Line
import java.io.BufferedInputStream; import java.io.FileInputStream; class Main { public static void main(String[] args) { try { // Creates a FileInputStream FileInputStream file = new FileInputStream("input.txt"); // Creates a BufferedInputStream BufferedInputStream input = new BufferedInputStream(file); // Reads first byte from file int i = input .read(); while (i != -1) { System.out.print((char) i); // Reads next byte from the file i = input.read(); } input.close(); } catch (Exception e) { e.getStackTrace(); } } } Output · First Line Second Line Third Line Fourth Line Fifth Line · In the a
🌐
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. To get more information about a file, use any of the File methods: import java.io.File; // Import the File class public class GetFileInfo { public static void main(String[] args) { File myObj = new File("filename.txt"); if (myObj.exists()) { System.out.println("File name: " + myObj.getName()); System.out.println("Absolute path: " + myObj.getAbsolutePath()); System.out.println("Writeable: " + myObj.canWrite()); System.out.println("Readable " + myObj.canRead()); System.out.println("File size in bytes " + myObj.length()); } else { System.out.println("The file does not exist."); } } }
🌐
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 - You have now explored several methods to read files line by line in Java. Each method has its own advantages. For example, BufferedReader offers straightforward line-by-line reading, Files.lines supports functional-style operations, while Scanner provides parsing capabilities.
🌐
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 - The only difference is that, the LineNumberReader class keeps track of the current line number, whereas the BufferedReader class does not.Reading all lines from a text file using the BufferedReader class is as easy as follows: String filePath = "Path/To/Your/Text/File.txt"; try { BufferedReader lineReader = new BufferedReader(new FileReader(filePath)); String lineText = null; while ((lineText = lineReader.readLine()) != null) { System.out.println(lineText); } lineReader.close(); } catch (IOException ex) { System.err.println(ex); }The above code snippet takes a given text file, reads every line and prints content of each line to the console output.
🌐
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 - Improve your proficiency by using BufferedReader class to read file contents line-by-line in Java. Read this article for beginners & experts!
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-read-a-large-text-file-line-by-line
Java Program to Read a Large Text File Line by Line - GeeksforGeeks
July 23, 2025 - A Scanner breaks its input into tokens, which by default matches the whitespace. ... // Java Program to Read a Large Text File Line by Line // Using Scanner class // Importing required classes import java.io.*; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.Scanner; // Main class public class GFG { // Main driver method public static void main(String[] args) throws FileNotFoundException { // Declaring and initializing the string with // custom path of a file String path = "C:\\Users\\H
🌐
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.
🌐
Java67
java67.com › 2012 › 10 › java-program-to-read-file-in-java.html
How to read a file line by line in Java? BufferedReader Example | Java67
Though you can read whole file byte by byte using FileInputStream, or you can directly read String from InputStream in Java, we have used BufferedReader to read file line by line for better performance.
🌐
GitHub
gist.github.com › 3032986
Read a file line by line java · GitHub
Read a file line by line java · Raw · lineByLine.java · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
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.
🌐
How to do in Java
howtodoinjava.com › home › i/o › java read file to string (with examples)
Java Read File to String (with Examples)
October 22, 2023 - The lines() method reads all lines from a file into a Stream. The Stream is populated lazily when the stream is consumed. Bytes from the file are decoded into characters using the specified charset.