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
- Start with Owner-only permission to read and write
- Do this Inspect the permission bits
- Result Owner can read and write
Syntax
public boolean canRead()
JDK 21 declarations (10)
public boolean canExecute()public boolean canRead()public boolean canWrite()public boolean setExecutable(boolean executable)public boolean setExecutable(boolean executable, boolean ownerOnly)public boolean setReadOnly()public boolean setReadable(boolean readable)public boolean setReadable(boolean readable, boolean ownerOnly)public boolean setWritable(boolean writable)public boolean setWritable(boolean writable, boolean ownerOnly)
Run these examples in Java
Save one example at a time as Main.java. Use JDK 21.
java Main.javaPrepare 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.
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);
}
}
}
}
true true true
Each setter reports whether the change was made. The following access query checks the current process on the test volume.
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.
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);
}
}
}
}
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?
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.