Set permission for Posix File
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.util.Set;
public class Test {
public static void main(String[] args) throws Exception {
Path path = Paths.get("home/docs/users.txt");
setPermission(path, PosixFilePermission.OWNER_EXECUTE);
}
private static void setPermission(Path path, PosixFilePermission permission)
throws Exception {
System.out.println("\nSetting permission for " + path.getFileName());
PosixFileAttributeView view = Files.getFileAttributeView(path,
PosixFileAttributeView.class);
PosixFileAttributes attributes = view.readAttributes();
Set<PosixFilePermission> permissions = attributes.permissions();
permissions.add(permission);
view.setPermissions(permissions);
System.out.println();
}
}
Related examples in the same category