Java PosixFileAttributeView set file group principal

Description

Java PosixFileAttributeView set file group principal

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.GroupPrincipal;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.UserPrincipal;
import java.nio.file.attribute.UserPrincipalLookupService;
import java.util.Set;

public class Main {

   public static void main(String[] args) {
      Path path = Paths.get("home/docs/users.txt");
      listPermissions(path);//from   www .ja v a 2  s  . c o m
      setGroupPrincipal(path, "mary", "mary");
      listPermissions(path);
   }

   private static void setGroupPrincipal(Path path, String userName, String groupName) {
      try {
         System.out.println("Setting owner for " + path.getFileName());
         PosixFileAttributeView view = Files.getFileAttributeView(path, PosixFileAttributeView.class);

         PosixFileAttributes attributes = view.readAttributes();
         System.out.println("Old Group: " + attributes.group().getName());
         System.out.println("Old Owner: " + attributes.owner().getName());
         System.out.println();

         UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
         UserPrincipal userPrincipal = lookupService.lookupPrincipalByName(userName);
         GroupPrincipal groupPrincipal = lookupService.lookupPrincipalByGroupName(groupName);
         view.setGroup(groupPrincipal);
         view.setOwner(userPrincipal);

         attributes = view.readAttributes();
         System.out.println("New Group: " + attributes.group().getName());
         System.out.println("New Owner: " + attributes.owner().getName());
         System.out.println();
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }

   private static void listPermissions(Path path) {
      try {
         System.out.println("Permission for " + path.getFileName());
         PosixFileAttributeView view = Files.getFileAttributeView(path, PosixFileAttributeView.class);
         PosixFileAttributes attributes = view.readAttributes();
         System.out.println("Group: " + attributes.group().getName());
         System.out.println("Owner: " + attributes.owner().getName());
         Set<PosixFilePermission> permissions = attributes.permissions();
         System.out.print("Permissions: ");
         for (PosixFilePermission permission : permissions) {
            System.out.print(permission.name() + " ");
         }
         System.out.println();
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }

}



PreviousNext

Related