Java Programs›File Handling›Read a File Line by Line

Read a File Line by Line in Java

beginner·  File Handling  ·  File I/O

Problem

Reading line by line keeps only the current line in memory, so it works for files of any size — unlike loading the whole file at once.

Read a short text file and print each line prefixed with its line number.

Input
poem.txt with 3 lines
Output
1: Roses bloom in May, ...

Java Program

Java
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; public class ReadFileLineByLine { public static void main(String[] args) throws IOException { Path file = Paths.get("poem.txt"); Files.write(file, Arrays.asList("Roses bloom in May,", "Rivers find the sea,", "Code compiles today.")); // sample input try (BufferedReader reader = new BufferedReader(new FileReader(file.toFile()))) { String line; int number = 1; while ((line = reader.readLine()) != null) { // null means end of file System.out.println(number++ + ": " + line); } } Files.delete(file); } }

Output

1: Roses bloom in May, 2: Rivers find the sea, 3: Code compiles today.

Core Logic

readLine() returns the next line each time it's called and null at the end of the file, which makes a natural loop condition.

How It Works
  1. 1The program first writes a three-line poem.txt so it has something to read.
  2. 2new BufferedReader(new FileReader(...)) opens the file inside try-with-resources.
  3. 3while ((line = reader.readLine()) != null) reads and tests in one expression.
  4. 4Each line is printed with a running line number, then the file is deleted.
The third call to readLine() returns Code compiles today., and the fourth returns null, ending the loop.
💡

Key Point: readLine() strips the line break, so the strings you receive never end in \n or \r\n.

Complexity
Time Complexity: O(n)Space Complexity: O(L)

Why: Every character is read once, and only the current line of length L is held at a time.

Key Concepts

BufferedReaderreadLine()try-with-resourceswhile loop

Approach 2: Using Files.lines()

Java
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Arrays; import java.util.stream.Stream; public class ReadFileLinesStream { public static void main(String[] args) throws IOException { Path file = Paths.get("poem.txt"); Files.write(file, Arrays.asList("Roses bloom in May,", "Rivers find the sea,", "Code compiles today.")); try (Stream<String> lines = Files.lines(file)) { // lazily reads lines; must be closed lines.filter(line -> line.startsWith("R")) .forEach(System.out::println); } Files.delete(file); } }

Output

Roses bloom in May, Rivers find the sea,

Core Logic

Files.lines exposes the file as a lazy Stream&lt;String&gt;, so stream operations such as filter apply directly to its lines.

How It Works
  1. 1Files.lines(file) opens the file and streams it one line at a time.
  2. 2The stream is opened in try-with-resources, because it holds an open file handle.
  3. 3filter(line -> line.startsWith("R")) keeps only lines beginning with R.
The first two lines start with R and are printed; the third is filtered out.
💡

Key Point: Forgetting to close a Files.lines stream leaks the file handle — always open it in try-with-resources.

Complexity
Time Complexity: O(n)Space Complexity: O(L)

Why: Lines are read lazily, one at a time, as the stream pulls them.

Key Concepts

Files.lines()Stream<String>filter()

Interview Questions

How do you read a file with a specific character encoding?

Pass the charset: Files.newBufferedReader(path, StandardCharsets.UTF_8). Relying on the platform default can garble non-English text.

What's the easiest way to read a small file at once?

Files.readString(path) (Java 11+) or Files.readAllLines(path). Both load the whole file into memory, so keep them for small files.

Learn the Concept

Related Programs