在Java中,我與在WinXP下運行的代碼創建這樣一個文件的工作:無法訪問Java創建的文件 - 有時
public synchronized void store(Properties props, byte[] data) {
try {
File file = filenameBasedOnProperties(props);
if (file.exists()) {
return;
}
File temp = File.createTempFile("tempfile", null);
FileOutputStream out = new FileOutputStream(temp);
out.write(data);
out.flush();
out.close();
file.getParentFile().mkdirs();
temp.renameTo(file);
}
catch (IOException ex) {
// Complain and whine and stuff
}
}
有時候,當創建一個文件這種方式,它是幾乎完全無法從代碼之外訪問(儘管負責打開和讀取文件的代碼沒有問題),即使應用程序沒有運行。當通過Windows資源管理器訪問時,我無法移動,重命名,刪除甚至打開文件。在Cygwin的,我得到了下面的時候我ls -l
目錄:
ls: cannot access [big-honkin-filename]
total 0
?????????? ? ? ? ? ? [big-honkin-filename]
所暗示,文件名都大了,但在260個字符的最大值爲XP(儘管它們是略微超過200個字符)。
爲了進一步增加我的電腦只是想讓我感到愚蠢的感覺,有時這個代碼創建的文件是完全正常的。我發現的唯一模式是,一旦目錄中的一個文件「鎖定」,剩下的就被擰緊了。
有人曾經遇到過這樣的事情,或者對這裏發生的事情有什麼見解?
(OT)當您即將關閉文件時,不需要刷新 - close()會執行所有需要的刷新。 – 2010-04-13 15:32:05
你應該檢查mkdirs的返回值和renameTo – LB40 2010-04-13 15:39:58