Files Exists NotExists Three State

exists() and notExists() are both false when accessibility is unknown, so pair them to distinguish definitely missing from cannot tell.

JavaData Integrity

Code

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

public class FilesExistsNotExists {

    public static void main(String[] args) throws IOException {
        Path present = Files.createTempFile("cfg-", ".tmp");
        Path missing = present.resolveSibling("missing.cfg");

        // exists / notExists 都是三态:true / false / 未知(也返回 false)
        System.out.println("exists(present)    = " + Files.exists(present));
        System.out.println("notExists(present) = " + Files.notExists(present));
        System.out.println("exists(missing)    = " + Files.exists(missing));
        System.out.println("notExists(missing) = " + Files.notExists(missing));

        // 两个结果相同 = 状态无法判定(如权限不足),不能当作“不存在”处理
        System.out.println("state known for present = "
                + (Files.exists(present) != Files.notExists(present)));
        System.out.println("definitely missing = "
                + (!Files.exists(missing) && Files.notExists(missing)));

        Files.delete(present);
    }
}

Inside the snippet

The snippet is organized around the class FilesExistsNotExists.

What to watch for

  • Free of dependencies by design — no registry entry, no lockfile line.

Using it

Output lines are prefixed (exists(present) =, notExists(present) =, exists(missing) =), and every step leaves one identifiable line behind.

Key statements

Path missing = present.resolveSibling("missing.cfg");
Path present = Files.createTempFile("cfg-", ".tmp");
Files.delete(present);

Interactive Tools for This Topic

FAQ

What does Files Exists NotExists Three State print when run?
Execute the whole file; each printed line is prefixed with a label like `exists(present) =`, `notExists(present) =`, `exists(missing) =`.