Java java.nio.file.Files getLastModifiedTime() Method
java.nio.file.Files class is an important utility class for file operations in the Java NIO (New I/O) package. Among them, getLastModifiedTime() method is a very practical method for obtaining 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 a
Pathobject, which can be accessed through thePaths.get()method creates
LinkOption... options
- Optional parameter used to specify how to handle symbolic links
- A commonly used option is
LinkOption.NOFOLLOW_LINKS, which means do not follow symbolic links - If this parameter is not provided, symbolic links are followed by default
Return Value
The method returns a FileTime object representing the last modified time of the file or directory.FileTime class provides various methods to handle time values:
toMillis(): Returns the number of milliseconds since 1970-01-01T00:00:00ZtoInstant(): ReturnsInstantobjecttoString(): Returns the time string in ISO-8601 format
Exception handling
The method may throw the following exceptions:
IOException: If an I/O error occursSecurityException: If there are not enough permissions to access the fileNullPointerException: 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
(
)
)
;
}
}
}
Do not follow symbolic links
Example
try
{
FileTime lastModifiedTime
=
Files.
getLastModifiedTime
(
path,
LinkOption.
NOFOLLOW_LINKS
)
;
// Process time...
}
catch
(
IOException
e
)
{
e.
printStackTrace
(
)
;
}
Practical Application Scenarios
- File synchronization tools: Detect whether a file has been modified to decide whether synchronization is needed
- Caching system: Check whether a resource file is updated to decide whether to refresh the cache
- Backup system: Back up only files modified since the last backup
- Build Tools: Check whether the source file is modified to decide whether recompilation is needed
Notes
- Different file systems may have different precision for "last modified time"
- On some operating systems, modifying file content does not necessarily update the directory's last modified time
- For symbolic links, the default behavior is to return the target file's last modified time
- Note synchronization issues when used in a multi-threaded environment
Performance considerations
getLastModifiedTime() The method's performance is usually very good because it only needs to access the file's metadata without reading the file content. However, performance may be affected in the following cases:
- Network file systems (NFS, SMB, etc.)
- When operating on a large number of files
- Environments with frequent security checks
Alternative comparison
The difference between File.lastModified() Comparison
java.io.File class also has a similar method lastModified(); 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 | Is | No |
| Symbolic link handling | Configurable | Always follows |
Recommended usage scenarios
- New code is recommended to use
Files.getLastModifiedTime() - Use when more precise time control is needed
Files.getLastModifiedTime() - Simple scenarios or legacy code maintenance can use
File.lastModified()
Summary
Files.getLastModifiedTime() is a simple but powerful method in Java NIO for obtaining the file system's last modified time information. Compared with traditional File.lastModified() , it provides better exception handling and more flexible time representation. When developing file-processing-related applications, using this method properly can improve code robustness and maintainability.
Java java.nio.file.Files