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

Java File setLastModified() Method

Detailed explanation of the Java File setLastModified() method

Java File Java File


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

- 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


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

  1. File existence check: before calling this method, you should first check whether the file exists
  2. Permission issues: ensure the program has sufficient permission to modify the target file
  3. File system limitations: some file systems may not support precise time settings
  4. Time precision: different operating systems may have different support for time precision
  5. 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:

How to ensure the time setting succeeds?

Suggested check steps:

  1. Check whether the file exists:file.exists()
  2. Check whether there is write permission:file.canWrite()
  3. called setLastModified() Then check the return value
  4. 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

  1. File backup system: preserve the original file's timestamp when restoring backups
  2. Test environment: simulate file modification times to test time-related business logic
  3. File synchronization tools: keep the timestamps of the source and target files consistent
  4. 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 Java File