Accessing File Attributes through View and Attribute Names – Java I/O: Part II

Accessing File Attributes through View and Attribute Names

The getAttribute() and setAttribute() methods of the Files class are general methods that can be used to read and update any file attribute by its name. These methods are useful when the Files class does not provide a specialized method for a particular file attribute.

The statement at (14) in Example 21.4 prints the group of the directory entry. The full attribute name of the group attribute is specified as “posix:group”, as the group attribute can be accessed via the POSIX file attribute view (p. 1336).

The statement at (20) in Example 21.4 sets the last access time of the directory entry to the current time. The attribute named “lastAccessTime” can be accessed via the basic file attribute view that is implied by default (p. 1334). The value of this file attribute is the FileTime object denoted by the timestamp reference.

Click here to view code image

static Object getAttribute(Path path, String attribute,
                           LinkOption… options) throws IOException
static Path   setAttribute(Path path, String attribute, Object value,
                           LinkOption… options) throws IOException

Read and set, respectively, the value of a file attribute of a directory entry denoted by the given Path object. By default, symbolic links are followed, unless the constant LinkOption.NOFOLLOW_LINKS is specified.

The attribute parameter has the general format:

view-name:attribute-name

where the view-name can be omitted. Typical view names are “basic”, “posix”, and “dos”. Omitting the view-name defaults to “basic”. The attribute-name is the name of the file attribute—for example, “lastModifiedTime” or “lastAccess-Time”. Attribute views are discussed in detail later (p. 1328).

The second method sets the file attribute to the value parameter.

Bulk Operations to Retrieve File Attributes

Accessing file attributes individually raises two concerns:

  • Accessing an individual file attribute incurs a cost, and frequent such accesses can adversely impact performance.
  • File attributes are very much file-system-specific, and therefore not conducive to generalizing over different file systems.

To address these concerns, the NIO.2 API provides bulk operations to retrieve file attributes—thus avoiding retrieval of individual file attributes and only handling file-system-specific attributes.

The following three approaches can be used to access file attributes of a directory entry in a file system:

  • The methods of the Files class can be used to access individual file attributes of a directory entry denoted by a Path, as discussed earlier (p. 1321).
  • A bulk operation using the readAttributes() method of the Files class can be used to retrieve a set of file attributes into a read-only object that implements a file attributes interface. This read-only object acts as a repository for file attribute values pertaining to the directory entry whose Path object was specified in the call to the readAttributes() method. The interface BasicFileAttributes, and its subinterfaces PosixFileAttributes and DosFileAttributes, define methods to read the retrieved file attribute values (Table 21.11, p. 1330).

In the code below at (1), the set of file attributes to read for the directory entry denoted by the path parameter is determined by the runtime object BasicFileAttributes.class.

Click here to view code image

BasicFileAttributes bfa = Files.readAttributes(path,                  // (1)
                                               BasicFileAttributes.class);

The retrieved values of the file attributes are accessible by querying the BasicFileAttributes object that is returned by the readAttributes() method (p. 1330).

  • A bulk operation using the getFileAttributeView() method of the Files class can be used to retrieve a set of file attributes into an updatable file attribute view that acts as a repository object. This object can be used to read or update selected file attributes pertaining to the directory entry whose Path object was specified in the call to the getFileAttributeView() method. The interface BasicFileAttributeView, and its subinterfaces PosixFileAttributeView and DosFileAttributeView, define methods to read and update the retrieved file attributes (Table 21.12, p. 1334).

In the code below at (6), the set of file attributes to retrieve for the directory entry denoted by the path parameter is determined by the runtime object Basic-FileAttributeView.class.

Click here to view code image

BasicFileAttributeView bfaView = Files.getFileAttributeView(path,      // (6)
                                                  BasicFileAttributeView.class);

The retrieved file attributes can be read and updated by querying the Basic-FileAttributeView object that is returned by the getFileAttributeView() method (p. 1334).

Details of the API for the readAttributes() and the getFileAttributeView() methods in the Files class are given below. By default, these methods follow symbolic links, unless the constant LinkOption.NOFOLLOW_LINKS is specified.

Click here to view code image

static <A extends BasicFileAttributes> A
       readAttributes(Path path, Class<A> type, LinkOption… options)
       throws IOException

Reads a set of read-only file attributes as a bulk operation for the directory entry denoted by the path parameter. The file attributes to retrieve are determined by the type parameter A. The parameter type is the Class<A> of the file attributes required to retrieve. The type parameter A is typically the interface BasicFileAttributes, or one of its subinterfaces PosixFileAttributes or DosFileAttributes (Table 21.11, p. 1330).

Click here to view code image

static <V extends FileAttributeView> V
       getFileAttributeView(Path path, Class<V> type, LinkOption… options)

Reads a set of file attributes of a file as a bulk operation. It returns a file attribute view of a given type, which can be used to read or update the retrieved file attribute values. The file attributes to retrieve are determined by the type parameter V. The parameter type is the Class<V> of the file attributes required to retrieve. The type parameter V is typically the interface BasicFileAttributeView, or one of its subinterfaces PosixFileAttributeView or DosFileAttributeView— that are all subinterfaces of the FileAttributeView interface (Table 21.12, p. 1334).

Click here to view code image

static Map<String,Object> readAttributes(Path path, String attributes,
                                         LinkOption… options)
                                         throws IOException

Reads a set of file attributes as a bulk operation for the directory entry denoted by the path parameter. It returns a map of file attribute names and their values.

The attributes parameter of type String has the general format:

view-name:attribute-list

where the view-name can be omitted, and attribute-list is a comma-separated list of attribute names. Omitting the view-name defaults to “basic”. For example, “*” will read all BasicFileAttributes, and “lastModifiedTime,lastAccessTime” will read only last modified time and last access time attributes.

Table 21.11 summarizes the interfaces that provide read-only access to file attribute values. The BasicFileAttributes interface defines the basic set of file attributes that are common to many file systems. Its two subinterfaces PosixFileAttributes and DosFileAttributes define additional file attributes associated with POSIX-based and DOS-based file systems, respectively. An object of the appropriate file attributes interface pertaining to a specific directory entry is returned by the Files.read-Attributes() method.

Table 21.11 Interfaces for Read-Only Access to File Attributes

Read-only file attributes interfaces in the java.nio.file.attribute package.Note that when an object of the read-only file attributes interface is created, it is opened on a specific directory entry in the file system. The object provides information about file attributes associated with this directory entry. The methods in these interfaces do not throw a checked exception.
BasicFileAttributesProvides read-only access to a basic set of file attributes that are common to many file systems (p. 1330).
PosixFileAttributes extends BasicFileAttributesIn addition to the basic set of file attributes, provides read-only access to file attributes associated with POSIX-based file systems (p. 1332).
DosFileAttributes extends BasicFileAttributesIn addition to the basic set of file attributes, provides read-only access to file attributes associated with DOS/ Windows-based file systems (p. 1333).
Categories: , ,

Leave a Reply

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