我目前在刪除一個我從未在我的程序中使用的文件時遇到問題。如果不調用System.gc(),File.delete()不會刪除新文件
首先,這裏是我的配置:
- Java版本:1.8.0_20
- 操作系統:Windows 7專業版SP1
的代碼如下:
File out = new File(workingDirectory, filePrefix + "download");
// cleanup old failed runs
//System.gc(); // Bad! but seems the only way to pass the test
boolean isDeleted = out.delete();
assertTrue("Couldn't clear output location ("
+ " isDeleted="+isDeleted
+ " exists="+out.exists()
+ " canWrite="+out.canWrite()
+ ")", !out.exists());
輸出錯誤跟蹤爲:
junit.framework.AssertionFailedError:
Couldn't clear output location (isDeleted=false exists=true canWrite=true)
at [...]
如果我取消註釋System.gc(),在我看來這是不好的,這個錯誤就解決了。看起來Windows即使從未使用過,也會在文件上保留一些資源。
我的問題是:
我怎樣才能解決這個問題,而無需使用System.gc()的?
由於通過預先
你是如何創建文件的?也許有一個未關閉的FileWriter? – 2015-03-31 14:18:09