Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

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

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

Java File Java java.nio.file.Files

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

LinkOption... options

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:

Exception handling

The method may throw the following exceptions:

  1. IOException: If an I/O error occurs
  2. SecurityException: If there are not enough permissions 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

(

)

)

;

       

}

   

}

}

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

  1. File synchronization tools: Detect whether a file has been modified to decide whether synchronization is needed
  2. Caching system: Check whether a resource file is updated to decide whether to refresh the cache
  3. Backup system: Back up only files modified since the last backup
  4. Build Tools: Check whether the source file is modified to decide whether recompilation is needed

Notes

  1. Different file systems may have different precision for "last modified time"
  2. On some operating systems, modifying file content does not necessarily update the directory's last modified time
  3. For symbolic links, the default behavior is to return the target file's last modified time
  4. 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:

  1. Network file systems (NFS, SMB, etc.)
  2. When operating on a large number of files
  3. 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


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 File Java java.nio.file.Files