Library contents

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

  1. Start with A regular file a.txt
  2. Do this Test whether it is a directory
  3. Result No
A regular file and a directory have different entry types.

Syntax

Java syntax
public static boolean exists(Path path, LinkOption... options)
JDK 21 declarations (8)
Run these examples in Java

Save one example at a time as Main.java. Use JDK 21.

java Main.java

Screen 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.

First output linetrue
Main.java
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);
            }
        }
    }
}
Output
true
true
true
false

The final value describes a newly created visible file on the tested local filesystem.

Advertisements

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.

First output linetrue
Main.java
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);
            }
        }
    }
}
Output
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?

Givenspring / alto.txt
Worked resultRegular file

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.

Documentation for Files entry and access checks