Java tutorial
import java.nio.file.attribute.PosixFilePermission; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.PosixFileAttributeView; import java.nio.file.attribute.PosixFileAttributes; import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermissions; import java.util.EnumSet; import java.util.Set; public class Main { public static void main(String[] args) throws Exception { Path path = Paths.get("test"); PosixFileAttributeView posixView = Files.getFileAttributeView(path, PosixFileAttributeView.class); if (posixView == null) { System.out.format("POSIX attribute view is not supported%n."); return; } readPermissions(posixView); updatePermissions(posixView); } public static void readPermissions(PosixFileAttributeView posixView) throws Exception { PosixFileAttributes attribs; attribs = posixView.readAttributes(); Set<PosixFilePermission> permissions = attribs.permissions(); // Convert the set of posix file permissions into rwxrwxrwx form String rwxFormPermissions = PosixFilePermissions.toString(permissions); System.out.println(rwxFormPermissions); } public static void updatePermissions(PosixFileAttributeView posixView) throws Exception { Set<PosixFilePermission> permissions = EnumSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.GROUP_READ); posixView.setPermissions(permissions); System.out.println("Permissions set successfully."); } }