Java File lastModified() Method
lastModified() is a method in Java's java.io.File an instance method provided by the class, used to obtain the timestamp of when the file was last modified. The method returns a long value representing the number of milliseconds since 00:00:00 GMT on January 1, 1970 (the Unix epoch).
Method Syntax
Example
public
long
lastModified
(
)
Return value description
- Return value type:
long - Return value meaning: the timestamp (in milliseconds) of the file's last modification
- Special return value: if the file does not exist or an I/O error occurs, returns
0L
Usage examples
Basic Usage
Example
import
java.io.File
;
public
class
LastModifiedExample
{
public
static
void
main
(
String
[
]
args
)
{
File
file
=
new
File
(
"example.txt"
)
;
long
lastModified
=
file.
lastModified
(
)
;
System
.
out
.
println
(
"File last modified timestamp: "
lastModified
)
;
}
}
Convert the timestamp to a readable format
Example
import
java.io.File
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
public
class
LastModifiedReadable
{
public
static
void
main
(
String
[
]
args
)
{
File
file
=
new
File
(
"example.txt"
)
;
long
lastModified
=
file.
lastModified
(
)
;
Date
date
=
new
Date
(
lastModified
)
;
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
)
;
System
.
out
.
println
(
"File last modified time: "
sdf.
format
(
date
)
)
;
}
}
Notes
Case where the file does not exist: if the file does not exist, the method returns 0L. Therefore, you should first check whether the file exists before using it:
Example
if
(
file.
exists
(
)
)
{
long
lastModified
=
file.
lastModified
(
)
;
// Process the timestamp
}
else
{
System
.
out
.
println
(
"File does not exist"
)
;
}
Permission issues: if the program does not have permission to read the file's metadata, the method may also return 0L.
Cross-Platform Behavior: different operating systems may define "last modified time" slightly differently, but it usually refers to the time the file content was last modified.
Precision Issues: some file systems may not record millisecond-precision timestamps.
Practical Application Scenarios
- File synchronization: compare the last modified times of two files to determine which one needs updating
- Caching mechanism: check whether a resource file has been modified to decide whether it needs to be reloaded
- Log analysis: analyze file modification time patterns to detect abnormal behavior
- Backup system: determine which files need to be backed up
Related methods
File.setLastModified(long time): set the file's last modified timeFile.length(): get the file sizeFile.exists(): check whether the file exists
Summary
lastModified() The method is a simple but practical tool in Java file operations; it provides basic information about the file modification time. By combining with Date and SimpleDateFormat class, we can convert these timestamps into more readable formats. In practical applications, reasonably utilizing file modification times can achieve many useful functions.
Java File