Library contents

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

  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 static Set<PosixFilePermission> getPosixFilePermissions(Path path, LinkOption... options) throws IOException
JDK 21 declarations (4)
Run these examples in Java

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

java Main.java

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

Resultrw-------
Main.java
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);
            }
        }
    }
}
Output
rw-------

The first three positions describe owner access. Group and other users receive no permission bits.

Advertisements

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.

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

GivenFile contents “clay”
Worked result600

What to watch for

Owners and permission views depend on the filesystem. Changing ownership may require extra privileges, even when a file is writable.

Documentation for Files POSIX permissions and owner