Library contents

Java File access flags

Query effective access and request changes to a file's read, write, or execute flags.

A simple example

File access flags

  1. Start with Owner-only permission to read and write
  2. Do this Inspect the permission bits
  3. Result Owner can read and write
On a POSIX filesystem the owner read and write bits correspond to mode 0600 when all other bits are clear.

Syntax

Java syntax
public boolean canRead()
JDK 21 declarations (10)
Run these examples in Java

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

java Main.java

Prepare a script for the workshop owner

A developer is packaging a local helper script for a workshop on a POSIX disk. The new owner should be able to read, edit and run the file, and the setup should report whether those requests succeeded.

First output linetrue
Main.java
import java.nio.file.*;
import java.io.*;

class Main {
    public static void main(String[] args) throws Exception {
        Path desk = Files.createTempDirectory("file-lesson-");
        try {
            File script = Files.createFile(desk.resolve("workshop.sh")).toFile();
            System.out.println(script.setReadable(true, true) && script.canRead());
            System.out.println(script.setWritable(true, true) && script.canWrite());
            System.out.println(script.setExecutable(true, true) && script.canExecute());
        } finally {
            try (var paths = Files.walk(desk)) {
                for (Path p : paths.sorted(java.util.Comparator.reverseOrder()).toList())
                    Files.deleteIfExists(p);
            }
        }
    }
}
Output
true
true
true

Each setter reports whether the change was made. The following access query checks the current process on the test volume.

Advertisements

Mark a submitted draft read-only

After collecting a draft, a tutor wants to discourage accidental edits on a local disk. Mark the practice file read-only, then restore its writable flag so the exercise can clean up.

First output linetrue
Main.java
import java.nio.file.*;
import java.io.*;

class Main {
    public static void main(String[] args) throws Exception {
        Path desk = Files.createTempDirectory("file-lesson-");
        try {
            File draft = Files.createFile(desk.resolve("draft.txt")).toFile();
            System.out.println(draft.setReadOnly());
            System.out.println(draft.setWritable(true));
        } finally {
            try (var paths = Files.walk(desk)) {
                for (Path p : paths.sorted(java.util.Comparator.reverseOrder()).toList())
                    Files.deleteIfExists(p);
            }
        }
    }
}
Output
true
true

The booleans describe successful requests on the tested volume. Read-only flags are not an authorization boundary.

Prepare a gallery label

A curator stores a short UTF-8 caption beside an exhibit. On a POSIX filesystem the owner sets read and write access for the owner alone. What are the resulting permission bits in octal?

GivenFile contents “clay”
Worked result600

What to watch for

Permission changes can fail or behave differently for privileged users and non-POSIX stores. Check the returned boolean before relying on a change.

Documentation for File access flags