Java Files POSIX permissions and owner
Read or change a file's POSIX mode and inspect its owning principal on a supporting store.
A simple example
Files POSIX permissions and owner
- Start with Owner-only permission to read and write
- Do this Inspect the permission bits
- Result Owner can read and write
Syntax
public static Set<PosixFilePermission> getPosixFilePermissions(Path path, LinkOption... options) throws IOException
JDK 21 declarations (4)
public static UserPrincipal getOwner(Path path, LinkOption... options) throws IOExceptionpublic static Set<PosixFilePermission> getPosixFilePermissions(Path path, LinkOption... options) throws IOExceptionpublic static Path setOwner(Path path, UserPrincipal owner) throws IOExceptionpublic static Path setPosixFilePermissions(Path path, Set<PosixFilePermission> perms) throws IOException
Run these examples in Java
Save one example at a time as Main.java. Use JDK 21.
java Main.javaRestrict a practice payroll export to its owner
A payroll assistant uses a POSIX volume for a local practice export. Before storing anything confidential, set the practice file to owner-only access and verify its symbolic permission string.
import java.nio.file.*;
import java.nio.file.attribute.*;
class Main {
public static void main(String[] args) throws Exception {
Path desk = Files.createTempDirectory("file-lesson-");
try {
Path payroll = Files.createFile(desk.resolve("practice.csv"));
Files.setPosixFilePermissions(payroll, PosixFilePermissions.fromString("rw-------"));
System.out.println(PosixFilePermissions.toString(Files.getPosixFilePermissions(payroll)));
} finally {
try (var paths = Files.walk(desk)) {
for (Path p : paths.sorted(java.util.Comparator.reverseOrder()).toList())
Files.deleteIfExists(p);
}
}
}
}
rw-------
The first three positions describe owner access. Group and other users receive no permission bits.
Preserve ownership during a controlled metadata test
A migration utility has received an ownership field along with an archived receipt. In a disposable local example, apply the file's existing owner to itself so the test does not need a second user account.
import java.nio.file.*;
class Main {
public static void main(String[] args) throws Exception {
Path desk = Files.createTempDirectory("file-lesson-");
try {
Path item = Files.createFile(desk.resolve("receipt"));
var owner = Files.getOwner(item);
Files.setOwner(item, owner);
System.out.println(Files.getOwner(item).equals(owner));
} finally {
try (var paths = Files.walk(desk)) {
for (Path p : paths.sorted(java.util.Comparator.reverseOrder()).toList())
Files.deleteIfExists(p);
}
}
}
}
true
The result verifies the same principal on the test volume. Other providers or accounts can reject this operation.
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
Owners and permission views depend on the filesystem. Changing ownership may require extra privileges, even when a file is writable.