Home Java Files Methods setPosixFilePermissions() Java Files setPosixFilePermissions() Method Definition In Java, Files.setPosixFilePermissions(path, perms) replaces all nine permission bits of a file, like chmod. Build the set with PosixFilePermissions.fromString("rwxr-xr-x") or from individual PosixFilePermission constants.
Unlike permissions passed when creating a file, this call is not reduced by the umask: you get exactly the bits you ask for. On Windows it throws UnsupportedOperationException.
A static method of the final utility class java.nio.file.Files, available since Java 7. Call it on the class name, as in Files.setPosixFilePermissions(...), after import java.nio.file.Files;. You never create a Files object.
Syntax Path and a set of permissions:
Files.setPosixFilePermissions(script, PosixFilePermissions.fromString("rwxr-xr-x"));
Files.setPosixFilePermissions(secret, EnumSet.of(OWNER_READ, OWNER_WRITE)); // rw------- Call What it does fromString("rw-r--r--")644: owner writes, all read. fromString("rwxr-xr-x")755: runnable by all. fromString("rw-------")600: private to the owner.
Since Java 7. Only the owner (or root) may change permissions. Returns the path.
Library declarations public static Path setPosixFilePermissions(Path path, Set<PosixFilePermission> perms) throws IOException Parameters Parameter Description pathThe file or folder. permsThe complete new set of permissions.
Return Value Return Value Description PathThe path you passed in.
Important
Throws UnsupportedOperationException on Windows, FileSystemException or AccessDeniedException if you are not the owner, and IllegalArgumentException from fromString for malformed text. It follows symbolic links and changes the target.
Compare with Similar Methods Permission methods:
Java Examples 1. Make a script runnable 2. Change one bit 3. Umask vs explicit set Example 1 — chmod 755 a Script Set rwxr-xr-x and confirm that the file is now executable.
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermissions;
public class SetPosixPermsExecutable {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path script = Files.write(dir.resolve("run.sh"), "echo hi\n".getBytes(StandardCharsets.UTF_8));
try {
Files.setPosixFilePermissions(script, PosixFilePermissions.fromString("rwxr-xr-x"));
System.out.println("Now: " + PosixFilePermissions.toString(Files.getPosixFilePermissions(script)));
System.out.println("Executable: " + Files.isExecutable(script));
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException: not a POSIX file system");
}
Files.delete(script);
Files.delete(dir);
}
} Run with java SetPosixPermsExecutable. The first part is the output on Linux or macOS; the last part shows Windows, which has no POSIX permissions.
Now: rwxr-xr-x
Executable: true
--- On Windows ---
UnsupportedOperationException: not a POSIX file system How It Works Text to set. fromString parses the nine letters; a dash means the bit is off.Exact result. Reading back shows rwxr-xr-x, because this call is not affected by the umask.The effect. With the owner's x bit set, isExecutable returns true and ./run.sh could be started from a shell.Example 2 — Add One Permission and Remove Another Copy the current set, edit it, and write it back.
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.EnumSet;
import java.util.Set;
public class SetPosixPermsEdit {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path file = Files.write(dir.resolve("tool"), new byte[0]);
if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) {
Files.setPosixFilePermissions(file, PosixFilePermissions.fromString("rw-r--r--"));
Set<PosixFilePermission> perms = EnumSet.copyOf(Files.getPosixFilePermissions(file));
perms.add(PosixFilePermission.OWNER_EXECUTE);
perms.remove(PosixFilePermission.OTHERS_READ);
Files.setPosixFilePermissions(file, perms);
System.out.println("Now: " + PosixFilePermissions.toString(Files.getPosixFilePermissions(file)));
} else {
System.out.println("No POSIX permissions on this system");
}
Files.delete(file);
Files.delete(dir);
}
} Run with java SetPosixPermsEdit. The first part is the output on Linux or macOS; the last part shows Windows, which has no POSIX permissions.
Now: rwxr-----
--- On Windows ---
No POSIX permissions on this system How It Works Like chmod u+x,o-r. There is no "add one bit" method, so read the set, change it, and set it again.A modifiable copy. EnumSet.copyOf guarantees a set you may change, whatever the file system returned.Result. rw-r--r-- plus the owner's x and minus others' r gives rwxr-----.Example 3 — Permissions at Creation Are Reduced by the umask Ask for rw-rw-rw- when creating a file, then set the same bits explicitly.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Set;
public class SetPosixPermsUmask {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Set<PosixFilePermission> wanted = PosixFilePermissions.fromString("rw-rw-rw-");
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(wanted);
try {
Path shared = Files.createFile(dir.resolve("shared.txt"), attr);
System.out.println("Created as: " + PosixFilePermissions.toString(Files.getPosixFilePermissions(shared)));
Files.setPosixFilePermissions(shared, wanted);
System.out.println("After set: " + PosixFilePermissions.toString(Files.getPosixFilePermissions(shared)));
Files.delete(shared);
} catch (UnsupportedOperationException e) {
System.out.println("POSIX initial attribute: UnsupportedOperationException");
}
Files.delete(dir);
}
} Run with java SetPosixPermsUmask. First part: Linux or macOS with the common umask 022 (the first line follows your umask). Last part: Windows.
Created as: rw-r--r--
After set: rw-rw-rw-
--- On Windows ---
POSIX initial attribute: UnsupportedOperationException How It Works Requested at creation. asFileAttribute passes rw-rw-rw- to createFile, but the operating system removes the bits in the umask.The usual umask, 022. It clears group and others' write, so the file is created as rw-r--r--. With umask 002 it would be rw-rw-r--.Explicit set wins. setPosixFilePermissions is not filtered, so the file ends up exactly rw-rw-rw-.Frequently Asked Questions How do I chmod a file in Java? Files.setPosixFilePermissions(path, PosixFilePermissions.fromString("rwxr-xr-x")) on Linux or macOS.
How do I make a file executable in Java on every OS? On Linux and macOS set an x bit as above; path.toFile().setExecutable(true) also works and does nothing harmful on Windows.
Why are my permissions different after createFile with asFileAttribute? The umask removes bits at creation time. Call setPosixFilePermissions afterwards to get the exact bits.
Does setPosixFilePermissions work on Windows? No, it throws UnsupportedOperationException. Use dos:readonly or ACLs on Windows.
Who may change a file's permissions? Its owner and root. Other users get FileSystemException or AccessDeniedException.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
15 people found this page helpful
Helpful Share Copy link Suggestion