我正在尝试遍历文件树并删除所有的文件/目录。以下是代码:
Files.walkFileTree(metricPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir,
IOException exc) throws IOException {
if (exc == null) {
Files.delete(dir);
return FileVisitResult.CONTINUE;
} else {
throw exc;
}
}
});
}
这段代码在单元测试之间运行,每个测试都会生成一个形如folder1/folder2/file
的单独文件。当我尝试遍历这个目录树时,当尝试删除folder1时,会抛出DirectoryNotEmptyException
异常,尽管它显然是空的...