Java Files exists() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 7+

Definition

In Java, Files.exists(path) returns true when a file, folder, or other entry exists at path, and false otherwise. It never throws IOException.

false means "does not exist or could not be checked", for example because of missing permissions. That is why exists and notExists are not exact opposites: both can be false.

A static method of the final utility class java.nio.file.Files, available since Java 7. Call it on the class name, as in Files.exists(...), after import java.nio.file.Files;. You never create a Files object.

Syntax

Pass a path and, optionally, LinkOption.NOFOLLOW_LINKS:

Java
boolean there = Files.exists(dir.resolve("todo.txt"));
boolean linkItself = Files.exists(link, LinkOption.NOFOLLOW_LINKS);
CallWhat it does
Files.exists(p)Follows symbolic links: is the final target there?
Files.exists(p, NOFOLLOW_LINKS)Is the entry itself there, even a dangling link?
Files.notExists(p)true only when absence is confirmed.

Since Java 7. The answer is only a snapshot: another program can create or delete the file right after the check. When you are about to act, prefer calls that fail clearly, such as createFile or deleteIfExists.

Library declarations
Java
public static boolean exists(Path path, LinkOption... options)

Parameters

ParameterDescription
pathThe path to test.
optionsLinkOption.NOFOLLOW_LINKS to test a symbolic link itself.

Return Value

Return ValueDescription
trueThe entry exists.
falseIt does not exist, or its existence could not be determined.

Compare with Similar Methods

Existence checks:

MethodHow it differs
notExists()Confirms absence; false when unsure.
isRegularFile()Exists and is a normal file.
isDirectory()Exists and is a folder.

Java Examples

Example 1 — Check a File Before and After Creating It

The same path gives a different answer once the file is created. Folders count too.

Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class ExistsBasic {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path todo = dir.resolve("todo.txt");
        System.out.println("todo.txt before: " + Files.exists(todo));
        Files.createFile(todo);
        System.out.println("todo.txt after: " + Files.exists(todo));
        System.out.println("temp folder: " + Files.exists(dir));
        Files.delete(todo);
        Files.delete(dir);
    }
}

How It Works

  1. A path is not a file. Building todo creates nothing, so the first check is false.
  2. The disk changed. After createFile the same Path object now reports true; the method asks the file system every time.
  3. Any kind of entry. The temp folder also exists, because exists does not care whether it is a file or a folder.

Example 2 — Write a Default Config Only on the First Run

Create a settings file with default values if it is missing, and keep the user's version once it exists.

Java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public class ExistsDefaultConfig {
    static String loadConfig(Path cfg) throws IOException {
        if (!Files.exists(cfg)) {
            Files.write(cfg, "volume=5".getBytes(StandardCharsets.UTF_8));
            System.out.println("Created default config");
        }
        return new String(Files.readAllBytes(cfg), StandardCharsets.UTF_8);
    }

    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path cfg = dir.resolve("app.cfg");
        System.out.println("Run 1: " + loadConfig(cfg));
        Files.write(cfg, "volume=9".getBytes(StandardCharsets.UTF_8));
        System.out.println("Run 2: " + loadConfig(cfg));
        Files.delete(cfg);
        Files.delete(dir);
    }
}

How It Works

  1. First run. app.cfg is missing, so the helper writes volume=5 and prints a note; that note appears before "Run 1" because loadConfig runs before println can print its line.
  2. The user edits it. The program then stores volume=9, as a user changing a setting would.
  3. Second run keeps it. Now exists is true, the default is skipped, and the user's value is returned. Two programs starting at the same moment could still race here; createFile closes that gap.

Example 3 — Test Files, Folders, and Impossible Paths

Check four names in a loop, including one that tries to go through a regular file as if it were a folder.

Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class ExistsSeveral {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path todo = Files.createFile(dir.resolve("todo.txt"));
        Path photos = Files.createDirectory(dir.resolve("photos"));
        String[] names = {"todo.txt", "photos", "missing.txt", "todo.txt/child"};
        for (String n : names) {
            System.out.println(n + " -> " + Files.exists(dir.resolve(n)));
        }
        Files.delete(todo);
        Files.delete(photos);
        Files.delete(dir);
    }
}

How It Works

  1. Forward slashes are fine. resolve("todo.txt/child") is accepted on Windows too, which treats / as a separator.
  2. Plain answers. The file and the folder report true; the name that was never created reports false.
  3. No exception for nonsense. A regular file cannot contain child, so the operating system reports an error internally, and exists turns it into false.

Frequently Asked Questions

Call Files.exists(path). It returns true if anything exists at that path and false otherwise. Use Files.isRegularFile(path) to require a normal file.
Not exactly. If Java cannot determine whether the file exists, for example because of permissions, both return false.
Yes, by default. A link to a missing target gives false. Pass LinkOption.NOFOLLOW_LINKS to check the link entry itself.
No checked exception. Problems while checking are reported as false.
Usually not. The answer can change right after the check. Call createFile, deleteIfExists, or the operation itself and handle its exception instead.

Next: Files.find()

Search a directory tree for paths matching a condition on name and attributes (Java 8+).

find() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful