FileNotFoundException

java.io.FileNotFoundException: /path/to/file.txt (No such file or directory)

Quick Answer

The specified file does not exist or the path is incorrect. Verify the file path and check that the file exists before opening it.

Why This Happens

FileNotFoundException is thrown when trying to open a file that does not exist, the path is a directory rather than a file, or the file cannot be opened for the requested access. Relative paths resolve from the working directory, which may not be what you expect.

The Problem

FileReader reader = new FileReader("data.txt"); // File doesn't exist

The Fix

Path path = Path.of("data.txt");
if (Files.exists(path)) {
    try (BufferedReader reader = Files.newBufferedReader(path)) {
        String content = reader.readLine();
    }
} else {
    System.err.println("File not found: " + path.toAbsolutePath());
}

Step-by-Step Fix

  1. 1

    Identify the file path

    Read the exception message for the exact path. Print the absolute path to see where Java is actually looking.

  2. 2

    Verify the path

    Check that the file exists at that location, the name is spelled correctly, and the extension is right.

  3. 3

    Fix the path or handle absence

    Use an absolute path, load from classpath with getResourceAsStream(), or check Files.exists() before opening.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free