Timestamp for Last Modification Time – Java I/O: Part II

Timestamp for Last Modification Time

Three different timestamps are associated with a directory entry, whose purpose is evident from their names: last modified time, last access time, and creation time. The timestamps are represented by the java.nio.file.attribute.FileTime class that provides the following methods for interoperability with Instant objects and with long values in milliseconds.

Click here to view code image

class java.nio.file.attribute.FileTime
static FileTime from(Instant instant)
static FileTime fromMillis(long value)

These static methods create a FileTime object representing the same point of time value on the timeline as the specified Instant object, or a FileTime object from the long value that specifies the number of milliseconds since the epoch (1970-01-01T00:00:00Z), respectively.

Instant toInstant()
long toMillis()

These instance methods convert this FileTime object to an Instant or to a long value in milliseconds from the epoch, respectively.

The Files class only provides static methods to read and update the last modified time of a directory entry. In Example 21.4, the statement at (9) prints the last modified time of the directory entry. The code at (15) sets the last modified time of the directory entry to the current time.

Click here to view code image

static FileTime getLastModifiedTime(Path path, LinkOption… options)
                                    throws IOException
static Path     setLastModifiedTime(Path path, FileTime time)
                                    throws IOException

Return or update the timestamp for the last modified time attribute of a directory entry, respectively. The timestamp is represented by the class java.nio .file.attribute.FileTime.

The first method follows symbolic links by default, unless the constant Link-Option.NOFOLLOW_LINKS is specified.

Accessing the Owner

The Files class only provides static methods to get and set the owner of a directory entry (i.e., one with a user account and appropriate access permissions). In Example 21.4, the statement at (10) prints the name of the owner of the directory entry. The code at (16) executes the necessary steps to obtain a user that can be set as the owner of the directory entry. This involves querying the file system to obtain the user look service and using the service to look up the user by name. We leave it to the reader to discover the exciting details from the API of the classes involved.

Click here to view code image

static UserPrincipal getOwner(Path path, LinkOption… options)
static Path setOwner(Path path, UserPrincipal owner)

Return or update the owner of a file, respectively.

Categories: , ,

Leave a Reply

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