Home Java Files Methods getPosixFilePermissions() Java Files getPosixFilePermissions() Method Definition In Java, Files.getPosixFilePermissions(path) returns the Unix permission bits of a file as a Set<PosixFilePermission>: values such as OWNER_READ, GROUP_WRITE, and OTHERS_EXECUTE.
PosixFilePermissions.toString(set) turns the set into the familiar nine-letter form, for example rwxr-x---. Windows has no POSIX permissions, so the call throws UnsupportedOperationException there.
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.getPosixFilePermissions(...), after import java.nio.file.Files;. You never create a Files object.
Syntax Path, then convert to text if you like:
Set<PosixFilePermission> perms = Files.getPosixFilePermissions(path);
String text = PosixFilePermissions.toString(perms); // e.g. "rw-r--r--"
boolean ownerRead = perms.contains(PosixFilePermission.OWNER_READ); Call What it does PosixFilePermissions.toString(s)Set to rwx text. PosixFilePermissions.fromString(t)rwx text to set. perms.contains(OWNER_EXECUTE)Test one bit.
Since Java 7. The set's iteration order is not specified; print it with PosixFilePermissions.toString or copy it into an EnumSet.
Library declarations public static Set<PosixFilePermission> getPosixFilePermissions(Path path, LinkOption... options) throws IOException Parameters Parameter Description pathThe file or folder. optionsNOFOLLOW_LINKS for a link itself.
Return Value Return Value Description Set<PosixFilePermission>The permission bits that are set.
Compare with Similar Methods Permission methods:
Java Examples 1. As rwx text 2. Enum order and octal 3. Security check Example 1 — Read Permissions as rwx Text Set known permissions first (defaults depend on the umask), then read and test them.
import java.io.IOException;
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.Set;
public class GetPosixPermsText {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path file = Files.write(dir.resolve("data.csv"), new byte[0]);
try {
Files.setPosixFilePermissions(file, PosixFilePermissions.fromString("rw-r-----"));
Set<PosixFilePermission> perms = Files.getPosixFilePermissions(file);
System.out.println("Permissions: " + PosixFilePermissions.toString(perms));
System.out.println("Owner can read: " + perms.contains(PosixFilePermission.OWNER_READ));
System.out.println("Others can read: " + perms.contains(PosixFilePermission.OTHERS_READ));
} catch (UnsupportedOperationException e) {
System.out.println("UnsupportedOperationException: no POSIX permissions");
}
Files.delete(file);
Files.delete(dir);
}
} Run with java GetPosixPermsText. The first part is the output on Linux or macOS; the last part shows Windows, which has no POSIX permissions.
Permissions: rw-r-----
Owner can read: true
Others can read: false
--- On Windows ---
UnsupportedOperationException: no POSIX permissions How It Works Known starting point. The default for a new file depends on the umask, so the example sets rw-r----- first.Text form. PosixFilePermissions.toString always writes owner, group, and others in rwx order.Single bits. contains tests one permission; others have no read bit here.Example 2 — Print the Set in Order and as an Octal Mode Copy the set into an EnumSet for a stable order and compute the chmod-style number.
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 GetPosixPermsOctal {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path tool = Files.write(dir.resolve("tool.sh"), new byte[0]);
if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) {
Files.setPosixFilePermissions(tool, PosixFilePermissions.fromString("rwxr-x---"));
Set<PosixFilePermission> perms = EnumSet.copyOf(Files.getPosixFilePermissions(tool));
System.out.println(perms);
int mode = 0;
for (PosixFilePermission p : perms) {
mode |= 1 << (8 - p.ordinal());
}
System.out.println("Octal: " + Integer.toOctalString(mode));
} else {
System.out.println("No POSIX permissions on this system");
}
Files.delete(tool);
Files.delete(dir);
}
} Run with java GetPosixPermsOctal. The first part is the output on Linux or macOS; the last part shows Windows, which has no POSIX permissions.
[OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, GROUP_READ, GROUP_EXECUTE]
Octal: 750
--- On Windows ---
No POSIX permissions on this system How It Works Stable order. The returned set has no defined order; an EnumSet always iterates in declaration order, owner first.Bits from the enum. The nine constants are declared from OWNER_READ (bit 8, octal 400) down to OTHERS_EXECUTE (bit 0), so 8 - ordinal gives each bit.chmod number. rwx is 7, r-x is 5, and --- is 0, so the mode is 750.Example 3 — Warn When a Private File Is Readable by Others Check the group and others bits of a key file and tighten them when needed.
import java.io.IOException;
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.Set;
public class GetPosixPermsAudit {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path key = Files.write(dir.resolve("id_key"), new byte[16]);
try {
Files.setPosixFilePermissions(key, PosixFilePermissions.fromString("rw-r--r--"));
Set<PosixFilePermission> perms = Files.getPosixFilePermissions(key);
boolean exposed = perms.contains(PosixFilePermission.GROUP_READ)
|| perms.contains(PosixFilePermission.OTHERS_READ);
System.out.println("Readable by others: " + exposed);
if (exposed) {
Files.setPosixFilePermissions(key, PosixFilePermissions.fromString("rw-------"));
}
System.out.println("Now: " + PosixFilePermissions.toString(Files.getPosixFilePermissions(key)));
} catch (UnsupportedOperationException e) {
System.out.println("Not a POSIX system: check ACLs instead");
}
Files.delete(key);
Files.delete(dir);
}
} Run with java GetPosixPermsAudit. The first part is the output on Linux or macOS; the last part shows Windows, which has no POSIX permissions.
Readable by others: true
Now: rw-------
--- On Windows ---
Not a POSIX system: check ACLs instead How It Works A risky default. rw-r--r-- lets every user on the machine read the key, which tools such as ssh refuse to accept.Two bits matter. The check looks at GROUP_READ and OTHERS_READ together.Tighten and confirm. After setting rw-------, only the owner can read or write the file.Frequently Asked Questions How do I read file permissions in Java? On Linux and macOS: PosixFilePermissions.toString(Files.getPosixFilePermissions(path)) gives text such as rw-r--r--.
Why does getPosixFilePermissions throw UnsupportedOperationException? The file system has no POSIX permissions, which is the case on Windows. Use isReadable/isWritable or ACLs there.
How do I get the octal mode like 755? Add up the bits: owner r/w/x = 400/200/100, group 40/20/10, others 4/2/1, or loop over the enum as shown above.
In what order does the returned set iterate? It is not specified. Copy it into an EnumSet or use PosixFilePermissions.toString for stable output.
What are the default permissions of a new file? Usually rw-r--r-- or rw-rw-r--, depending on the process umask.
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.
14 people found this page helpful
Helpful Share Copy link Suggestion