Java Read Files

Reading a file brings its stored text back into your program. Java offers several readers, from Scanner for line-by-line reading to Files.readAllLines and Files.readString for quick loads. Choosing the right one depends on how big the file is and what you plan to do with it.

Reading is the reverse of writing: you pull characters out of a file and use them in your program. A common beginner-friendly tool is the Scanner class, which can read a file one line at a time. For small files you can also load everything at once with the Files helper methods. Whichever you choose, be ready for the file to be missing, which throws an exception.

Reading line by line with Scanner

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    try {
      File file = new File("story.txt");
      Scanner reader = new Scanner(file);
      while (reader.hasNextLine()) {
        String line = reader.nextLine();
        System.out.println(line);
      }
      reader.close();
    } catch (FileNotFoundException e) {
      System.out.println("The file was not found.");
    }
  }
}

The hasNextLine method returns true as long as there is another line to read, and nextLine gives you that line as a String. This loop pattern reads the whole file without loading it all into memory at once, which is why Scanner is a good choice for large files.

Loading a whole file at once

When a file is small and you want its contents quickly, the Files class can hand you everything in one call. Files.readString returns the entire file as a single String, while Files.readAllLines returns a List where each element is one line.

Reading everything with Files

import java.nio.file.Path;
import java.nio.file.Files;
import java.io.IOException;
import java.util.List;

public class Main {
  public static void main(String[] args) throws IOException {
    Path path = Path.of("story.txt");

    // Whole file as one String
    String all = Files.readString(path);
    System.out.println("Characters: " + all.length());

    // Or as a list of lines
    List<String> lines = Files.readAllLines(path);
    for (String line : lines) {
      System.out.println(line);
    }
  }
}
ToolReadsBest for
ScannerOne line (or token) at a timeLarge files, streaming
Files.readAllLinesAll lines into a ListSmall files you loop over
Files.readStringThe whole file as one StringSmall files you treat as one blob
Note: Reading a missing file throws FileNotFoundException (a checked exception) with Scanner, or NoSuchFileException with the Files methods. Always handle these so a typo in a filename does not crash your whole program.
Note: For very large files, avoid readAllLines and readString because they load everything into memory at once. Stream the file line by line with Scanner or Files.lines(path) instead.
  • Scanner reads a file line by line, ideal for large files.
  • Files.readAllLines returns each line as an element of a List.
  • Files.readString loads the entire file into one String.
  • Reading can fail if the file is missing, so handle the exception.

Exercise: Java File Handling

What does creating a new File("data.txt") object actually do?