Detailed explanation of the Java File setLastModified() method
setLastModified() is a method in Java's java.io.File a method provided by the class, used to set the last modified time of a file or directory. This method is very useful in actual development, for example when you need to simulate a file's modification time or synchronize a file's timestamp.
Method Syntax
public
boolean
setLastModified
(
long
time
)
Method parameters
time parameter
- Type:long
- Meaning: represents the number of milliseconds since 00:00:00 GMT on January 1, 1970 (i.e., the Unix timestamp)
- Notes:
- If a negative time is passed in, the method throws
IllegalArgumentException - Time precision depends on the support of the underlying file system (some systems may only be precise to the second)
Return Value
- Return Type:boolean
- true: indicates that the file's last modified time was set successfully
- false: indicates the operation failed, possibly because the file does not exist, there is no write permission, or the file system does not support this operation
Usage examples
Basic Usage
Example
import
java.io.File
;
public
class
FileTimeExample
{
public
static
void
main
(
String
[
]
args
)
{
File
file
=
new
File
(
"example.txt"
)
;
// Get the current time as the new modification time
long
newTime
=
System
.
currentTimeMillis
(
)
;
// Set the file's last modified time
boolean
success
=
file.
setLastModified
(
newTime
)
;
if
(
success
)
{
System
.
out
.
println
(
"File modification time updated successfully"
)
;
}
else
{
System
.
out
.
println
(
"File modification time update failed"
)
;
}
}
}
Set a specific date
Example
import
java.io.File
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
public
class
SpecificDateExample
{
public
static
void
main
(
String
[
]
args
)
throws
Exception
{
File
file
=
new
File
(
"report.doc"
)
;
// Create a specific date (January 1, 2023)
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd"
)
;
Date
specificDate
=
sdf.
parse
(
"2023-01-01"
)
;
// Convert to millisecond timestamp
long
timeInMillis
=
specificDate.
getTime
(
)
;
// Set the file modification time
if
(
file.
setLastModified
(
timeInMillis
)
)
{
System
.
out
.
println
(
"Successfully set the file modification time to January 1, 2023"
)
;
}
}
}
Notes
- File existence check: before calling this method, you should first check whether the file exists
- Permission issues: ensure the program has sufficient permission to modify the target file
- File system limitations: some file systems may not support precise time settings
- Time precision: different operating systems may have different support for time precision
- Symbolic links: if the file is a symbolic link, this method affects the actual file the link points to
Frequently Asked Questions
Why does setLastModified() return false?
Possible reasons include:
- File does not exist
- No write permission
- The file system does not support modifying timestamps
- The passed-in time parameter is invalid
How to ensure the time setting succeeds?
Suggested check steps:
- Check whether the file exists:
file.exists() - Check whether there is write permission:
file.canWrite() - called
setLastModified()Then check the return value - called
file.lastModified()Verify whether the time has been updated
Does this method affect the file's creation time?
No.setLastModified() It only affects the last modified time and does not change the file's creation time. In the standard Java API, there is no direct method to modify a file's creation time.
Practical Application Scenarios
- File backup system: preserve the original file's timestamp when restoring backups
- Test environment: simulate file modification times to test time-related business logic
- File synchronization tools: keep the timestamps of the source and target files consistent
- Archiving system: set a uniform modification time for archived files
By mastering setLastModified() Using this method, you can handle file time-related operations more flexibly, adding more practical features to your Java file processing program.
Java File