2

I am dealing with a file in Java that is used to hold serialized objects. In order to determine if the process that is writing to this file has timed out, I check the lastModified() method on the file and expect it to be updated each time an object is written. On Linux and Mac this works fine, but on Windows, the timestamp is not updated until the FileOutputStream is closed. I've also tried directly updating the timestamp using setLastModified() to no avail. From what I've gleaned from Google, it seems that this is known behavior with files on Windows.

My question is how can I achieve what I want on Windows? I just need to be able to determine when something was serialized. The rub is that the serialization and the monitoring are in different JVMs.

I'd prefer to find a way to keep using the timestamp approach as it works great on all other platforms, but workarounds would also be appreciated.

Thanks

4

2 回答 2

1

If you're using Java 7, you can use java.nio.file.Files and its SetLastModifiedTime(Path, FileTime) static method.

I'm not sure if that works any differently than File's setLastModified, though.

于 2012-12-20T20:05:32.363 回答
0

If you have control of the source code for the writing process why no just write/touch another file too instead?

new File(otherFile.getParent(), "touch_file").setLastModified(Sytem.currentTimeMillis());

Not sure if that work or if you need to open it and close it instead:

new FileOutputStream(new File(otherFile.getParent(), "touch_file"))).close();

By the way depending on filesystem etc on Linux the lastModifiedTime on files might only have a 1 second resolution and precision.

于 2012-12-20T22:20:15.460 回答