<分区>
我目前在删除一个我从未在我的程序中使用过的文件时遇到问题。
首先,这是我的配置:
- Java 版本:1.8.0_20
- 操作系统:Windows 7 Pro 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 在文件上保留了一些资源,即使它从未被使用过。
我的问题是:
How can I solve this problem without using System.gc() ?
提前致谢