我目前遇到一个问题,就是在我的程序中有一个我从未使用过的文件无法删除。
首先,这是我的配置:
- 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仍然保留了一些资源。
我的问题是:
如何在不使用System.gc()的情况下解决这个问题?
提前感谢。