Java Files notExists() Method

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

Definition

In Java, Files.notExists(path) returns true only when Java has confirmed that nothing exists at path. If the entry exists, or if the check itself fails (for example, a folder on the way cannot be searched), it returns false.

So a path is in one of three states: exists is true (it is there), notExists is true (it is not), or both are false (unknown).

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.notExists(...), after import java.nio.file.Files;. You never create a Files object.

Syntax

Same arguments as exists:

Java
if (Files.notExists(target)) {
    // safe to assume nothing is there right now
}
CallWhat it does
Files.notExists(p)Confirmed absent (following links).
Files.notExists(p, NOFOLLOW_LINKS)The entry itself, even a link, is absent.
!Files.exists(p)Absent or unknown: a different question.

Since Java 7. Use notExists when wrongly assuming "absent" would be harmful, for example before choosing a file name to create.

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

Parameters

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

Return Value

Return ValueDescription
trueNothing exists there (confirmed).
falseIt exists, or its status is unknown.

Compare with Similar Methods

Existence checks compared:

MethodHow it differs
exists()Confirmed present; false when absent or unknown.
deleteIfExists()Acts instead of asking, so no race.
createFile()Atomic "create only if absent".

Java Examples

Example 1 — Describe a Path with exists and notExists Together

A helper combines both checks into "exists", "does not exist", or "unknown".

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

public class NotExistsDescribe {
    static String describe(Path p) {
        if (Files.exists(p)) {
            return "exists";
        }
        if (Files.notExists(p)) {
            return "does not exist";
        }
        return "unknown";
    }

    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path file = Files.createFile(dir.resolve("data.csv"));
        System.out.println("data.csv: " + describe(file));
        System.out.println("temp folder: " + describe(dir));
        System.out.println("old.csv: " + describe(dir.resolve("old.csv")));
        Files.delete(file);
        Files.delete(dir);
    }
}

How It Works

  1. Ask the positive question first. If exists is true, the answer is settled.
  2. Then the negative one. old.csv was never created, so Java can confirm its absence and notExists returns true.
  3. The leftover case. Only when both checks are false does the helper say unknown, the state that a single !exists test would hide.

Example 2 — Make the Status Unknown with Permissions

Remove search permission from a folder on Linux or macOS. Neither method can see inside, so both return false.

Java
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermissions;

public class NotExistsUnknown {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path locked = Files.createDirectory(dir.resolve("locked"));
        Path secret = Files.createFile(locked.resolve("secret.txt"));
        if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) {
            Files.setPosixFilePermissions(locked, PosixFilePermissions.fromString("rw-------"));
            System.out.println("exists: " + Files.exists(secret));
            System.out.println("notExists: " + Files.notExists(secret));
            Files.setPosixFilePermissions(locked, PosixFilePermissions.fromString("rwx------"));
        } else {
            System.out.println("No POSIX permissions on this system");
        }
        Files.delete(secret);
        Files.delete(locked);
        Files.delete(dir);
    }
}

How It Works

  1. Check support first. supportedFileAttributeViews() contains "posix" on Linux and macOS but not on Windows, so the program never calls a method that would throw UnsupportedOperationException.
  2. Lock the folder. rw------- removes the x bit, which on a folder means "may look up names inside", so secret.txt can no longer be checked.
  3. Both say no. The lookup fails with "permission denied", so exists cannot confirm presence and notExists cannot confirm absence.
  4. Unlock before cleanup. Restoring rwx------ lets the program delete secret.txt and the folders.

Example 3 — Choose report.txt, report-1.txt, report-2.txt...

Find the first name that is confirmed free. An unknown status is treated as taken, which is the safe choice.

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

public class NotExistsFreeName {
    static Path freeName(Path dir, String base, String ext) {
        Path p = dir.resolve(base + ext);
        for (int i = 1; !Files.notExists(p); i++) {
            p = dir.resolve(base + "-" + i + ext);
        }
        return p;
    }

    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path first = Files.createFile(dir.resolve("report.txt"));
        Path second = Files.createFile(dir.resolve("report-1.txt"));
        Path next = freeName(dir, "report", ".txt");
        System.out.println("Next free name: " + next.getFileName());
        Files.delete(first);
        Files.delete(second);
        Files.delete(dir);
    }
}

How It Works

  1. Two names are taken. report.txt and report-1.txt already exist.
  2. Loop until confirmed free. The loop keeps going while notExists is false, which covers both "exists" and "unknown".
  3. First confirmed gap. report-2.txt is confirmed absent, so it is returned. To create it safely, follow up with createFile, which fails if another program grabbed the name first.

Frequently Asked Questions

exists returns true when the file is confirmed present; notExists returns true when it is confirmed absent. If the status cannot be determined, both return false.
When Java cannot check the path, most often because permissions forbid looking inside a folder on the way, as in Example 2.
No. !exists(p) is true when the file is absent or its status is unknown; notExists(p) is true only when absence is confirmed.
Yes. For a link to a missing target it returns true. With LinkOption.NOFOLLOW_LINKS it checks the link itself, which exists.
No. Errors during the check simply lead to false.

Next: Files.probeContentType()

Guess a file's MIME type, such as text/plain or image/png; may return null.

probeContentType() 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.

14 people found this page helpful