Java Files entry and access checks
Inspect the kind and current accessibility of a file or directory.
A simple example
Files entry and access checks
- Start with A regular file a.txt
- Do this Test whether it is a directory
- Result No
Syntax
public static boolean exists(Path path, LinkOption... options)
JDK 21 declarations (8)
public static boolean exists(Path path, LinkOption... options)public static boolean isDirectory(Path path, LinkOption... options)public static boolean isExecutable(Path path)public static boolean isHidden(Path path) throws IOExceptionpublic static boolean isReadable(Path path)public static boolean isRegularFile(Path path, LinkOption... options)public static boolean isWritable(Path path)public static boolean notExists(Path path, LinkOption... options)
Run these examples in Java
Save one example at a time as Main.java. Use JDK 21.
java Main.javaScreen ordinary applications in a drop folder
A registrar expects a normal document in a local application inbox. Before displaying it, classify the directory and its file, check for a missing second upload and inspect the ordinary file's visibility.
import java.nio.file.*;
class Main {
public static void main(String[] args) throws Exception {
Path desk = Files.createTempDirectory("file-lesson-");
try {
Path application = Files.writeString(desk.resolve("entry.txt"), "accepted");
System.out.println(Files.exists(application) && Files.isRegularFile(application));
System.out.println(Files.isDirectory(desk));
System.out.println(Files.notExists(desk.resolve("second.txt")));
System.out.println(Files.isHidden(application));
} finally {
try (var paths = Files.walk(desk)) {
for (Path p : paths.sorted(java.util.Comparator.reverseOrder()).toList())
Files.deleteIfExists(p);
}
}
}
}
true true true false
The final value describes a newly created visible file on the tested local filesystem.
Inspect the access flags on a class folder
A lab assistant runs a desktop program under the account that created its local workspace. On a POSIX disk, the program checks whether it can read, write and search that directory before it displays its status.
import java.nio.file.*;
class Main {
public static void main(String[] args) throws Exception {
Path desk = Files.createTempDirectory("file-lesson-");
try {
System.out.println(Files.isReadable(desk));
System.out.println(Files.isWritable(desk));
System.out.println(Files.isExecutable(desk));
} finally {
try (var paths = Files.walk(desk)) {
for (Path p : paths.sorted(java.util.Comparator.reverseOrder()).toList())
Files.deleteIfExists(p);
}
}
}
}
true true true
For a directory on POSIX, execute permission allows traversal. These results describe the account and disk used for this run.
Locate a rehearsal score
The choir stores each score inside a season folder. A clerk creates an ordinary file at the given location. Which entry type should the status check report?
What to watch for
exists and notExists can both be false when a result cannot be determined. Access can change before an actual open, which still needs error handling.