The PosixFileAttributeView Interface – Java I/O: Part II

The PosixFileAttributeView Interface

As the PosixFileAttributeView interface is a subinterface of the BasicFileAttribute-View interface, it allows both the basic set of file attributes and the POSIX-specific file attributes to be read and updated.

Click here to view code image

interface PosixFileAttributeView
          extends BasicFileAttributeView, FileOwnerAttributeView
String name()

Returns the name of the attribute view, which in this case is the string “posix”.

Click here to view code image

PosixFileAttributes readAttributes()

Retrieves the basic and POSIX-specific file attributes as a bulk operation into a PosixFileAttributes object whose methods can be used to read the values of these file attributes (p. 1332). This method is analogous to the readAttributes() method of the Files class (p. 1328).

Click here to view code image

void setGroup(GroupPrincipal group) throws IOException

Updates the group of the directory entry. Note that there is no analogous method in the Files class for handling the group.

Click here to view code image

void setPermissions(Set<PosixFilePermission> perms)

Updates the file permissions. This method is analogous to the Files.setPosixFilePermissions() method (p. 1325).

The PosixFileAttributeView interface extends the java.nio.file.attribute.File-OwnerAttributeView interface that defines the methods for reading and updating the owner of the directory entry. See also analogous methods relating to ownership in the Files class (p. 1325).

Click here to view code image

interface java.nio.file.attribute.FileOwnerAttributeView
          extends FileAttributeView
UserPrincipal getOwner() throws IOException
void          setOwner(UserPrincipal owner) throws IOException

Return or update the owner of a directory entry, respectively. These methods are analogous to the methods in the Files class (p. 1325).

A PosixFileAttributeView object can thus read both the basic set of file attributes and the POSIX-specific file attributes, and can update the owner, group, file permissions, and timestamps for the last modified, last access, and creation times for a directory entry.

The code below obtains a PosixFileAttributeView object at (13) that pertains to the file denoted by the path reference. The associated PosixFileAttributes object is obtained at (14), providing read-only access to the basic file attributes and the POSIX-specific file attributes, whose values are printed by calling the methods printBasicFileAttributes() and printPosixFileAttributes() in the utility class FileUtils, respectively.

Click here to view code image

Path path = Path.of(“project”, “src”, “pkg”, “Main.java”);
out.println(“File: ” + path);
PosixFileAttributeView pfaView = Files.getFileAttributeView(path,      // (13)
                                              PosixFileAttributeView.class);
System.out.printf(“Using view: %s%n”, pfaView.name());
// Reading the basic + POSIX set of file attributes:                   // (14)
PosixFileAttributes pfa2 = pfaView.readAttributes();
FileUtils.printBasicFileAttributes(pfa2);
FileUtils.printPosixFileAttributes(pfa2);
// Updating owner and group file attributes using view.                // (15)
FileSystem fs = path.getFileSystem();
UserPrincipalLookupService upls = fs.getUserPrincipalLookupService();
UserPrincipal newUser = upls.lookupPrincipalByName(“javadude”);
GroupPrincipal newGroup = upls.lookupPrincipalByGroupName(“admin”);
pfaView.setOwner(newUser);
pfaView.setGroup(newGroup);
//Updating file permissions using view.                                // (16)
Set<PosixFilePermission> newPerms = PosixFilePermissions.fromString(“r–r–r–“);
pfaView.setPermissions(newPerms);
//Updating last access time using view.                                // (17)
FileTime currentAccessTime = pfa2.lastAccessTime();
long newLATinMillis = currentAccessTime.toMillis() + 10*60*1000L;
FileTime newLastAccessTime = FileTime.fromMillis(newLATinMillis);
pfaView.setTimes(null, newLastAccessTime, null);
// Reading the updated file attributes:                                // (18)
pfa2 = pfaView.readAttributes();
FileUtils.printBasicFileAttributes(pfa2);
FileUtils.printPosixFileAttributes(pfa2);

The code from (15) to (17) shows how the PosixFileAttributeView object can be used to update various file attributes. Keep in mind that this view inherits from the BasicFileAttributeView and the FileOwnerAttributeView interfaces.

The code at (15) updates the owner and the group of the directory entry via the view. An owner and a group are looked up in the appropriate lookup services, and updated by the setOwner() and setGroup() methods of the PosixFileAttributeView interface. See also corresponding methods in the Files class (p. 1325).

The code at (16) updates the file permissions of the directory entry via the view, analogous to the Files.setPosixFilePermissions() method (p. 1325). File permissions are set to read-only for the owner, the group, and other users.

The code at (17) updates only the last access time of the directory entry via the view, analogous to updating the last modified time via the BasicFileAttributeView object (p. 1334).

Updated file attribute values can be read using the appropriate methods of the Files class, or by obtaining a new PosixFileAttributes object, as shown at (18).

Categories: ,

Leave a Reply

Your email address will not be published. Required fields are marked *