我使用JPoller來檢測特定目錄中文件的更改,而是因爲他們結束了一個時間戳早於他們的實際創建它的文件丟失時間。以下是我測試:file.createNewFile()創建的文件與實際創建時間之前的最後一次修改時間
public static void main(String [] files)
{
for (String file : files)
{
File f = new File(file);
if (f.exists())
{
System.err.println(file + " exists");
continue;
}
try
{
// find out the current time, I would hope to assume that the last-modified
// time on the file will definitely be later than this
System.out.println("-----------------------------------------");
long time = System.currentTimeMillis();
// create the file
System.out.println("Creating " + file + " at " + time);
f.createNewFile();
// let's see what the timestamp actually is (I've only seen it <time)
System.out.println(file + " was last modified at: " + f.lastModified());
// well, ok, what if I explicitly set it to time?
f.setLastModified(time);
System.out.println("Updated modified time on " + file + " to " + time + " with actual " + f.lastModified());
}
catch (IOException e)
{
System.err.println("Unable to create file");
}
}
}
這裏就是我得到的輸出:
-----------------------------------------
Creating test.7 at 1272324597956
test.7 was last modified at: 1272324597000
Updated modified time on test.7 to 1272324597956 with actual 1272324597000
-----------------------------------------
Creating test.8 at 1272324597957
test.8 was last modified at: 1272324597000
Updated modified time on test.8 to 1272324597957 with actual 1272324597000
-----------------------------------------
Creating test.9 at 1272324597957
test.9 was last modified at: 1272324597000
Updated modified time on test.9 to 1272324597957 with actual 1272324597000
結果是一個競爭條件:
- JPoller記錄最後一次檢查以xyz的時間.. .123
- 在xyz創建的文件... 456
- 文件最後修改時間戳實際上讀取xyz ... 000
- JPoller查找帶有時間戳比XYZ新的更大的/更新的文件... 123
- JPoller忽略新加入的文件,因爲XYZ ... 000小於XYZ ... 123
- 我拉我的頭髮了一會兒
我試圖挖掘到的代碼,但兩者lastModified()
和createNewFile()
最終下定決心,本地調用,所以我留下了很少的信息。對於test.9
,我輸了957毫秒。我可以期待什麼樣的準確度?我的結果會因操作系統或文件系統而異?建議的解決方法?
注:我目前正在與XFS文件系統上運行Linux。我在C
寫了一個快速程序和stat system call顯示st_mtime
作爲truncate(xyz...000/1000)
。
更新:我跑了相同的節目,我上面有在Windows 7上使用NTFS,它不保持充分的誤差在毫秒級。@mdma提供了進一步的說明,FAT文件系統對於以10毫秒分辨率創建時是準確的,但訪問僅準確到2秒。因此,這完全取決於操作系統。
這可能與某人有關......當在Linux和javas上使用XFS lastModified()時,它始終以相同的時間返回(1.8.0_131-b11) – Zarathustra 2017-06-12 16:01:30