“java.nio.file.NoSuchFileException” Explained with Easy Fixes

Alexey Karimov

Trying to read from a file, but Java throws java.nio.file.NoSuchFileException error?

This error usually means the file or directory path you’ve referenced doesn’t actually exist when the JVM tries to access it. It’s a checked exception from the java.nio.file package, and often appears when using methods like Files.readAllBytes(), Files.walkFileTree(), or any operation that relies on a valid Path.

This guide explains why it happens and how to fix it quickly, with examples.

Why java.nio.file.NoSuchFileException Happens and How To Fix It?

This exception can stem from more than just a missing file. Sometimes, it’s a typo in the path, a mismatch in encoding, or even assumptions about where your code is running from. 

Here are 5 common reasons why “java.nio.file.NoSuchFileException” gets triggered, and how to fix each.

1. The File or Path Doesn’t Exist

Path path = Paths.get("C:/nonexistent/data.txt");
byte[] content = Files.readAllBytes(path); // Throws NoSuchFileException

This is the most common case. If the file or folder you’re trying to access doesn’t exist at runtime, Java immediately throws NoSuchFileException. To fix it, verify the path and confirm that the file actually exists before accessing it.

2. Directory Path Is Wrong or Misspelled

Path path = Paths.get("C:/wrongFolder/file.txt");
Files.newBufferedReader(path); // Throws NoSuchFileException

Even a small typo in a folder name will break the path resolution. Unlike File.exists(), most NIO methods don’t silently fail, but throw this exception if any part of the path is invalid.

3. Relative Paths Like data/sample.txt

Path path = Paths.get("data/sample.txt");
Files.size(path); // May throw NoSuchFileException

Relative paths like data/sample.txt depend on the current working directory. If your code runs from a different base (like an IDE vs. terminal), the file might not be found. Use System.getProperty(“user.dir”) to debug the actual base path if needed.

String baseDir = System.getProperty("user.dir");
Path path = Paths.get(baseDir, "data/sample.txt");
if (Files.exists(path)) {
    long size = Files.size(path);
}

4. File Deleted or Moved After Path Was Created

Path path = Paths.get("C:/temp/file.txt");
Files.delete(path);
// Later in code
Files.readAllBytes(path); // Throws NoSuchFileException

Paths are just references. If the file gets deleted or moved before it’s used, an exception will be thrown when the method tries to resolve it.

Here’s a simple fix for it: Check whether the file still exists at the time of access, not just when the path was created. This avoids runtime failures due to unexpected deletions or moves.

Path path = Paths.get("C:/temp/file.txt");
if (Files.exists(path)) {
    byte[] data = Files.readAllBytes(path);
} else {
    System.out.println("File was deleted or moved.");
}

5. Wrong Encoding on Non-ASCII Filenames

Path path = Paths.get("C:/docs/çonfig.txt");
Files.size(path); // May throw NoSuchFileException

On some systems, non-ASCII characters in filenames may be misinterpreted if your Java source file or terminal doesn’t use UTF-8 encoding. Make sure your .java files are saved with UTF-8 encoding, and avoid pasting special characters unless they match the OS code page.

Wrapping Up

Most java.nio.file.NoSuchFileException cases trace back to one root cause: the specified path doesn’t resolve to a valid resource at runtime.

As a better practice, always verify paths before using them in file operations. Use Files.exists(path) for critical reads, log System.getProperty(“user.dir”) when debugging relative paths, and avoid hardcoded non-ASCII filenames unless you’ve confirmed encoding support.