Java java.nio.file.Files getLastModifiedTime()Method

Java File Java java.nio.file.Files

java.nio.file.FilesThe Files class is an important utility class for file operations in the Java NIO (New I/O) package. ThegetLastModifiedTime()method is a very practical method, used to obtain the last modified time of a file or directory.

Method Definition

public static FileTime getLastModifiedTime(Path path, LinkOption... options)
    throws IOException

Parameter Description

Path path

  • Represents the path of the file or directory to query
  • This is aPathobject, which can be created using thePaths.get()method

LinkOption... options

  • Optional parameter, used to specify how to handle symbolic links
  • The common option isLinkOption.NOFOLLOW_LINKS, indicating not to follow symbolic links
  • If this parameter is not provided, symbolic links are followed by default

Return Value

The method returns aFileTimeobject, representing the last modified time of the file or directory.FileTimeThe class provides multiple methods to handle time values:

  • toMillis(): Returns the number of milliseconds since 1970-01-01T00:00:00Z
  • toInstant(): Returns anInstantobject
  • toString(): Returns the time string in ISO-8601 format

Exception Handling

The method may throw the following exceptions:

  1. IOException: If an I/O error occurs
  2. SecurityException: If there is not enough permission to access the file
  3. NullPointerException: If the path parameter is null

Usage Examples

Basic Usage

Example

import java.nio.file.*;
import java.io.IOException;

public class LastModifiedExample {
    public static void main(String[] args) {
        Path path = Paths.get("example.txt");
       
        try {
            FileTime lastModifiedTime = Files.getLastModifiedTime(path);
            System.out.println("Last modified time: " + lastModifiedTime);
            System.out.println("Milliseconds since epoch: " + lastModifiedTime.toMillis());
        } catch (IOException e) {
            System.err.println("Error getting last modified time: " + e.getMessage());
        }
    }
}

Not Following Symbolic Links

Example

try {
    FileTime lastModifiedTime = Files.getLastModifiedTime(
        path,
        LinkOption.NOFOLLOW_LINKS
    );
    // Handle time...
} catch (IOException e) {
    e.printStackTrace();
}

Real-world Application Scenarios

  1. File synchronization tools: Detect whether a file has been modified, and decide whether synchronization is needed
  2. Cache systems: Check whether resource files have been updated, and determine whether the cache needs to be refreshed
  3. Backup systems: Only back up files modified since the last backup
  4. Build tools: Check whether source files have been modified, and decide whether recompilation is needed

Notes

  1. Different file systems may have different precision for the "last modified time"
  2. On some operating systems, modifying file content may not update the last modified time of the directory
  3. For symbolic links, the default behavior is to return the last modified time of the target file
  4. When using in a multi-threaded environment, attention should be paid to synchronization issues

Performance Considerations

getLastModifiedTime()The performance of the method is usually good, because it only needs to access the file's metadata without reading the file content. However, there may be performance impacts in the following cases:

  1. Network file systems (NFS, SMB, etc.)
  2. When performing a large number of file operations
  3. Environments with many security checks

Comparison of Alternatives

andFile.lastModified()Comparison

java.io.FileThe class also has a similar methodlastModified(), the main differences are as follows:

Feature Files.getLastModifiedTime() File.lastModified()
Return value type FileTime long (milliseconds)
Exception handling Throws IOException Returns 0 to indicate an error
NIO support Yes no
Symbolic link handling Configurable Always follows

Recommended Usage Scenarios

  • For new code, it is recommended to useFiles.getLastModifiedTime()
  • Use when more precise time control is neededFiles.getLastModifiedTime()
  • Can be used in simple scenarios or for maintaining legacy codeFile.lastModified()

Summary

Files.getLastModifiedTime()is a simple but powerful method in Java NIO, used to obtain the last modified time information of the file system. Compared with the traditionalFile.lastModified()it provides better exception handling and a more flexible way of representing time. When developing applications related to file handling, using this method appropriately can improve the robustness and maintainability of the code.

Java File Java java.nio.file.Files

Other Extensions