Skip to main content

Can i rely on the File.lastModified() to detect file modification??

10 replies [Last post]
dextml
Offline
Joined: 2007-09-19

Despite the Security Manager, can i detect a file modification just via its last modification time WITHOUT bothering walking thru every byte of that file?
The modification time is the ret value of File.lastModified().

The detection don't need to be theoretically accurate. If the possible failure rate is below 0.00001%, the approach can be considered safe and successful.

If this holds true, the file change detection speed can be boosted at a great level.

Thx.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mthornton
Offline
Joined: 2003-06-10

Many applications do assume that changes imply a change to the modification time. Most of the time this is correct, but it is possible to change a file without changing the mod time. Also there may be a delay between changes and the mod time being updated. It often won't be updated until the file is closed.

dextml
Offline
Joined: 2007-09-19

Can u explain how to avoid the change on modification time? By finely tuning the system time ... at millisecond level?

If the file is in use, what's the effective way to detect this? By calling File.canWrite()?

Thanks for your help.

mthornton
Offline
Joined: 2003-06-10

You can simply change a file and then alter the modification time back to its previous value. It also seems to be possible to alter a file via a memory mapping without affecting its modification date (on Windows anyway). To determine if another task has a file open for writing you could attempt to obtain a lock on the file.

dextml
Offline
Joined: 2007-09-19

Last question, Why is it that obtaining a file lock is preferable to testing write privilege (via File.canWrite) when i am going to declare some file is currently in use?

Much appreciated.

mthornton
Offline
Joined: 2003-06-10

File.canWrite determines if you have permission to write it, but does not check if any other process currently has write access or a lock.

dextml
Offline
Joined: 2007-09-19

Can't i assume that the file is currently in use if i can't obtain the write permission?

tarbo
Offline
Joined: 2006-12-18

No. Permissions are not related to the current condition of the file or any open streams. Permissions decide whether you as a user, or part of a user group, have the right to read, write, or execute a certain file. Whether the file is currently in use is another question.

dextml
Offline
Joined: 2007-09-19

Sorry, this is clearly a misinterpreting. What i mean when i mentioned the write permission is the value returned by File.canWrite(). True for permitted, False for denial.

mthornton
Offline
Joined: 2003-06-10

File.canWrite can return true even if an attempt to open the file for writing would fail because another process has locked the file. The only way to ensure that a file is not being modified by another process is to acquire a lock on the file (and even that isn't guaranteed on systems that don't implement mandatory locks).

dextml
Offline
Joined: 2007-09-19

Much appreciated