3

File's last modified time is changed only when the file is closed.

public class Main {
    public static void main(String[] args) throws IOException {
        File f = new File("xyz.txt");

        FileWriter fwr = new FileWriter(f);
        System.out.println(f.lastModified());
        fwr.write("asasdasdasd");
        System.out.println(f.setLastModified(System.currentTimeMillis()));
        fwr.flush();
        System.out.println(f.lastModified());
        fwr.close();
        System.out.println(f.lastModified());
        System.out.println(f.setLastModified(System.currentTimeMillis()));      
    }
}

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 variable - 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. )

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
raj
  • 3,769
  • 4
  • 25
  • 43

3 Answers3

3

Depending on what you actually want to achieve one of two methods may be appropriate:

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!

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • 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 '11 at 09:55
  • Edited my code.. added the setLastModified.. when i use that function when the file is open, the method returns false. – raj Apr 19 '11 at 09: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 '11 at 09:59
2

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.

Ingo
  • 36,037
  • 5
  • 53
  • 100
  • 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 '11 at 09:57
0

What about using setLastModified()?

Dakshinamurthy Karra
  • 5,353
  • 1
  • 17
  • 28
  • Edited my code.. added the setLastModified.. when i use that function when the file is open, the method returns false. – raj Apr 19 '11 at 09:56
  • As Joachim and Ingo rightly pointed out, Its really OS/ Filesystem dependent. – raj Apr 19 '11 at 10:04