Changing File's last modified without closing it?

Please keep in mind that the behaviour you see is highly dependend on OS and perhaps file system type. Hence, java can not and does not specify when file times are updated. Therefore, no , there is no portable way to do that in java.

Thats Fair.! I remember, the native Filesystem is the one who finally decides when to write the file/ change the modified date.. Hmmm.. – raj Apr 19 at 9:57.

Depending on what you actually want to achieve one of two methods may be appropriate: use File.setLastModified() to directly manipulate the timestampe or use f.flush() to ensure all data written is actually written to the disk. Note that the OS and/or the file system may have a lower resolution of that timestamp than you hope for. For example FAT stores those timestamps with a 2-second resolution!

Even more modern file systems are known to store only in a single-second resolution. Also note that the behavior of the last modified timestamp varies a lot depending on the OS. On my Ubuntu, for example, only the write/flush modifies the timestamp, the close() doesn't!

1 Not only that - recently I watched a program that wrote a multi-MB file under Windows7 to an NTFS partition - yet it appeared to be 0 bytes long until the program finished. – Ingo Apr 19 at 9:55 Edited my code.. added the setLastModified.. when I use that function when the file is open, the method returns false. – raj Apr 19 at 9:55 1 @raj: Windows has traditionally had a very single-process view on editing files: as long as one process has a file open it's pretty much unusable for others.

This seems to extend to the timestamps as well. Note that as far as I know there is a way to open files non-blockingly in Windows, it's just not common (and probably not easily accessible from Java). – Joachim Sauer Apr 19 at 9:59.

Edited my code.. added the setLastModified.. when I use that function when the file is open, the method returns false. – raj Apr 19 at 9:56 It is working OK on OSX 10.6. – KDM Apr 19 at 10:02 As Joachim and Ingo rightly pointed out, Its really OS/ Filesystem dependent. – raj Apr 19 at 10:04.

File's last modified time is changed only when the file is closed. Hmm.. now, in my actual program, a file is opened and one of the thread keeps writing the file. Several other threads need to know when was any data last written to the file.

Is there any possible way update last modified without closing the file? ( I know, having a static vaiable - long lastWriteTime in the thread that writes the file would work. But just curious to know if there is any other way, to change the last modified time without closing the file.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions