๐ŸŽ“ A1Learner
๐Ÿ”
0 of 40 lessons completed

Java Reading Line by Line

A BufferedReader reads a text file one line at a time. The readLine method returns each line in turn, and null when the file ends.

Number the lines as they are read

The program writes three colour lines first. The readLine loop prints each line with its number.

import java.io.BufferedReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
public class Main {
  public static void main(String[] args) throws Exception {
    Files.write(Path.of("colours.txt"), List.of("Red", "Green", "Blue"));
    try (BufferedReader reader = Files.newBufferedReader(Path.of("colours.txt"))) {
      String line;
      int number = 1;
      while ((line = reader.readLine()) != null) {
        System.out.println(number + ": " + line);
        number++;
      }
    }
  }
}

Write a numbered copy, then read it back

The reader feeds each line to a writer, which numbers it. Reading the copy back whole shows the numbered lines.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
public class Main {
  public static void main(String[] args) throws Exception {
    Files.write(Path.of("colours.txt"), List.of("Red", "Green", "Blue"));
    try (BufferedReader reader = Files.newBufferedReader(Path.of("colours.txt"));
         BufferedWriter writer = Files.newBufferedWriter(Path.of("colours-copy.txt"))) {
      String line;
      int number = 1;
      while ((line = reader.readLine()) != null) {
        writer.write(number + ": " + line);
        writer.newLine();
        number++;
      }
    }
    System.out.print(Files.readString(Path.of("colours-copy.txt")));
  }
}

Example

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
public class Main {
  public static void main(String[] args) throws Exception {
    Files.write(Path.of("tasks.txt"), List.of("Wash dishes", "Water plants", "Take out trash"));
    try (BufferedReader reader = Files.newBufferedReader(Path.of("tasks.txt"))) {
      String line;
      int number = 1;
      while ((line = reader.readLine()) != null) {
        System.out.println(number + ": " + line);
        number++;
      }
    }
    try (BufferedReader reader = Files.newBufferedReader(Path.of("tasks.txt"));
         BufferedWriter writer = Files.newBufferedWriter(Path.of("tasks-copy.txt"))) {
      String line;
      int number = 1;
      while ((line = reader.readLine()) != null) {
        writer.write(number + ": " + line);
        writer.newLine();
        number++;
      }
    }
    System.out.print(Files.readString(Path.of("tasks-copy.txt")));
  }
}

Key points

  • Files.newBufferedReader builds a BufferedReader for a path.
  • The readLine method returns the next line of the file.
  • The loop ends when readLine returns null, the signal for the end of the file.
  • A BufferedReader is a resource, so it belongs inside try-with-resources.
  • A BufferedWriter writes text line by line, and newLine ends each line.
  • Reading the written copy back whole proves the copy matches the original.

In this example, the program first prints the task file with numbered lines from a readLine loop. It then writes a numbered copy with a BufferedWriter and reads the copy back whole, and the two numbered printouts match line for line.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does readLine return when the file has no more lines?