症状很简单:Windows拒绝删除一个目录,因为它认为它的内容仍然存在 - 虽然递归删除只是删除它们。所以我的第一个猜测是在删除它的内容之后并在删除它之前,父目录上出现了冲突/同步或类似的缺失。没有可靠的递归目录删除可能吗?
我试着公地IO 2.5版FileUtils.deleteDirectory和FileUtils.cleanDirectory功能,以及我自己的简化测试:
@Test
public void testMySimpleDelete() throws IOException, InterruptedException {
File dir = TEST_DIR;
for (int i = 0; i < 10; i++) {
dir = new File(dir, Integer.toString(i));
}
for (int i = 0; i < 1000; i++) {
LOG.info(""+i);
assertTrue("loop #" + i, dir.mkdirs());
mySimpleDelete(Paths.get(TEST_DIR.getAbsolutePath()));
assertFalse(TEST_DIR.exists());
}
}
private void mySimpleDelete(Path file) throws IOException, InterruptedException {
Files.walkFileTree(file, new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
//LOG.info(dir.toString());
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
}
无不显示出相同的结果/症状。
如果你想自己验证这个行为(仅限于Windows!Linux根本不锁定任何东西,并且行为方式更加宽松),只需检查https://github.com/jjYBdx4IL/java-evaluation并运行“mvn test -Dtest = org.apache .commons.io.FileUtilsTest”。
参见http://stackoverflow.com/q/22948189/886887 –