Java Set File Permissions using PosixFilePermission With Examples

Java File class has the ability to set the file permissions but it’s not versatile. The biggest drawback is that you can divide file permissions into two sets of users – owner and everybody else only. You can’t set different file permissions for group and other users.

Java Set File Permissions

Java 7 has introduced PosixFilePermission Enum and java.nio.file.Files includes a method setPosixFilePermissions(Path path, Set<PosixFilePermission> perms) that can be used to set file permissions easily.

Here is a simple program that clearly shows why PosixFilePermission is better.

FilePermissions.java

From the above example, it’s clear that PosixFilePermission provides more options and code is more readable. If you are using Java 7 or above, you should use it for setting file permissions in java.

By admin

Leave a Reply