Java Reference
In-Depth Information
// Change the file owner to brice
UserPrincipal newOwner =
upls.lookupPrincipalByName("brice");
foav.setOwner(newOwner);
UserPrincipal changedOwner = foav.getOwner();
System.out.format("New owner of %s is %s%n",
path, changedOwner.getName());
}
catch (UnsupportedOperationException | IOException e) {
e.printStackTrace();
}
}
}
Original owner of C:\poems\luci1.txt is CORPORATE\ksharan
New owner of C:\poems\luci1.txt is CORPORATE\brice
The following snippet of code uses the Files.setOwner() method to update the owner of a file identified with
the path C:\poems\luci1.txt on Windows:
UserPrincipal owner = get the owner;
Path path = Paths.get("C:\\poems\\luci1.txt");
Files.setOwner(path, owner);
Managing ACL File Permissions
In this section, I will cover managing the file permissions using AclFileAttributeView . Note that ACL type file
attributes are supported on Microsoft Windows. An ACL consists of an ordered list of access control entries. Each
entry consists of a UserPrincipal , the type of access, and the level of the access to an object. In NIO.2, an instance of
the AclEntry class represents an entry in an ACL. You can get and set a List of AclEntry for a file using the getAcl()
and setAcl() methods of the AclFileAttributeView . The following snippet of code gets the List of ACL entries for a
file called C:\poems\luci1.txt :
Path path = Paths.get("C:\\poems\\luci1.txt");
AclFileAttributeView view =
Files.getFileAttributeView(path, AclFileAttributeView.class);
List<AclEntry> aclEntries = view.getAcl();
The AclEntry class has methods to read various properties of an ACL entry. Its principal() method returns the
UserPrincipal to identify the user or the group. Its permissions() method returns a Set of AclEntryPermission
objects to identify the permissions. Its type() method returns an enum constant of the type AclEntryType such as
ALARM , ALLOW , AUDIT , and DENY that indicates the type of the access. Its flags() method returns a Set of AclEntryFlag
enum constants, which contains the inheritance flags of the ACL entry.
Listing 10-17 demonstrates how to read ACL entries for file C:\poems\luci1.txt . If the file does not exist, a
NoSuchFileException is thrown. The program handles the exception and prints the stack trace of the exception. If you
run the program on a UNIX-like platform, it will print an error message that the ACL view is not supported. A partial
output is shown when the program was run on Windows. You may get a different output.
 
Search WWH ::




Custom Search