开发者

File.createTempFile create special file or how to force flush data to harddrive?

I'm using File.createTempFile to create regular files I want to keep, the reason I use this method is because it guarantees a unique file name. However I'm seeing a strange thing with files created by this method: After I flush and closed the output stream on this file, I crash the machine running JVM deliberately, I assumed since the stream is flushed and closed, the file should contain valid data. However, sometimes the file gets filled with 0x0 instead (Note I'm testing this on a VMWare box running WinXP and latest Sun JVM 1.6).

Is the problem caused by the temp file or is开发者_运维百科 this some kind of general problem which applies to all Java file io? What should I do to ensure the data gets flushed to harddrive?

Thanks


In addition to flush(), try calling output.getFileDescriptor().sync() on your FileOutputStream.


Most modern filesystem access is subject to this sort of problem - see about filesystem journaling for a quick introduction to the problem.

The short of it is - if your system crashes, recently updated files may revert to previous data. If you recently created the file, it may be zero-filled.

On UNIX you prevent this using fsync and/or fdatasync. I'm not a Windows guy but this might be a starting point: cross platform fsync(). I'm not sure how to do it from Java.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜