Java Files set posix file permissions

Description

Java Files set posix file permissions

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;

public class Main {

   public static void main(String[] args) {
      Path path = Paths.get("home/docs/users.txt");
      try {// w w  w  . jav a 2 s  .c o m
         Set<PosixFilePermission> permissions = Files.getPosixFilePermissions(path);
         System.out.print("Permissions: ");
         for (PosixFilePermission permission : permissions) {
            System.out.print(permission.name() + " ");
         }
         permissions.remove(PosixFilePermission.GROUP_EXECUTE);
         Files.setPosixFilePermissions(path, permissions); 

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }

}



PreviousNext

Related