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

Java Reading Files

Files.readString reads a whole text file into one String in a single call. Files.readAllLines reads the same file as a list of lines instead.

Read the whole file as one String

The program writes three lines first, then reads them back in one call. The printed text matches the written lines exactly.

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("notes.txt"), List.of("Pens", "Pencils", "Rulers"));
    String text = Files.readString(Path.of("notes.txt"));
    System.out.print(text);
  }
}

Read the lines as a list

readAllLines answers a list, so its size counts the lines. A loop then prints the lines in file order.

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("notes.txt"), List.of("Pens", "Pencils", "Rulers"));
    List<String> lines = Files.readAllLines(Path.of("notes.txt"));
    System.out.println(lines.size());
    for (String line : lines) {
      System.out.println(line);
    }
  }
}

A missing file is caught

The name missing.txt is never written, so the read fails. The catch block prints a plain message, and no path text appears.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
public class Main {
  public static void main(String[] args) {
    try {
      String text = Files.readString(Path.of("missing.txt"));
      System.out.println(text);
    } catch (NoSuchFileException e) {
      System.out.println("No file with that name exists.");
    } catch (IOException e) {
      System.out.println("The file could not be read.");
    }
  }
}

Example

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("shopping.txt"), List.of("Milk", "Bread", "Eggs"));
    String text = Files.readString(Path.of("shopping.txt"));
    System.out.print(text);
    List<String> lines = Files.readAllLines(Path.of("shopping.txt"));
    System.out.println(lines.size());
    for (String line : lines) {
      System.out.println(line);
    }
  }
}

Key points

  • Files.readString returns the whole file as one String, line breaks included.
  • Files.readAllLines returns a List of Strings, one element per line.
  • The size of the readAllLines list is the line count of the file.
  • Reading a file that does not exist throws NoSuchFileException.
  • A catch block for NoSuchFileException can print a plain message of the programโ€™s own.

In this example, the program writes a three-line shopping list, then reads it back twice. The readString print shows the whole text, and the readAllLines size of 3 counts the lines before the loop prints them in order.

Note

Every program in this lesson writes its file first, so the file it reads always holds known content.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which method reads a whole text file into one String?