java - 无法删除空文件夹
问题描述
在现有文件夹中,我创建了一个文件夹,并使用此方法在其中创建了多个文件:
SeekableByteChannel createFile(String filePathToCreate) throws IOException {
OpenOption[] options = {
StandardOpenOption.WRITE,
StandardOpenOption.CREATE_NEW,
StandardOpenOption.SPARSE,
StandardOpenOption.READ
// TODO: think if we add CREATE if exist rule.
};
return Files.newByteChannel(Paths.get(filePathToCreate), options);
}
文件夹/文件结构为:
- torrents-test
- folder1
- File-I-Created-1
- File-I-Created-2
- File-I-Created-3
然后我尝试torrents-test
使用这种方法删除文件夹:
void deleteDirectory(File directoryToBeDeleted) throws IOException {
File[] allContents = directoryToBeDeleted.listFiles();
if (allContents != null) {
for (File file : allContents) {
deleteDirectory(file);
}
}
Files.delete(directoryToBeDeleted.toPath());
}
然后我得到一个异常,告诉我该文件夹folder1
不是空的,所以我不能删除它:
java.nio.file.DirectoryNotEmptyException: C:\GIT\university\torrentx\torrents-test\folder1
at sun.nio.fs.WindowsFileSystemProvider.implDelete(WindowsFileSystemProvider.java:266)
at sun.nio.fs.AbstractFileSystemProvider.delete(AbstractFileSystemProvider.java:103)
at java.nio.file.Files.delete(Files.java:1126)
at com.utils.Utils.deleteDirectory(Utils.java:389)
at com.utils.Utils.deleteDirectory(Utils.java:386)
at com.utils.Utils.deleteDownloadFolder(Utils.java:375)
at com.utils.Utils.removeEverythingRelatedToTorrent(Utils.java:87)
at com.steps.MyStepdefs.applicationCreateActiveTorrentFor(MyStepdefs.java:297)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at cucumber.runtime.Utils$1.call(Utils.java:40)
at cucumber.runtime.Timeout.timeout(Timeout.java:16)
at cucumber.runtime.Utils.invoke(Utils.java:34)
at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:38)
at cucumber.runtime.StepDefinitionMatch.runStep(StepDefinitionMatch.java:37)
at cucumber.runtime.Runtime.runStep(Runtime.java:300)
at cucumber.runtime.model.StepContainer.runStep(StepContainer.java:44)
at cucumber.runtime.model.StepContainer.runSteps(StepContainer.java:39)
at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:44)
at cucumber.runtime.model.CucumberScenarioOutline.run(CucumberScenarioOutline.java:46)
at cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)
at cucumber.runtime.Runtime.run(Runtime.java:122)
at cucumber.api.cli.Main.run(Main.java:36)
at cucumber.api.cli.Main.main(Main.java:18)
我的deleteDirectory
方法首先删除它尝试删除的每个文件夹中的所有文件,然后才会删除该文件夹。该异常表明删除该文件夹中的每个文件都是成功的,因为如果没有成功,我会在尝试删除该文件夹的文件之一时更早遇到异常。
我的问题是- 为什么我会得到那个例外?
解决方案
的javadocjava.nio.file.Files.delete(Path path)
很清楚:
如果文件是目录,则目录必须为空。
它还指出:
此方法可与 walkFileTree 方法一起使用,以删除目录和目录中的所有条目,或在需要时删除整个文件树。
使用Files.walkFileTree()
会使您的代码更清晰、更短,但请注意,它不会解决您的实际问题。
除了删除所有资源的递归方法是正确的,因为您从较深的开始并备份较不深的资源来删除资源。
问题出在其他地方:确实,您正在创建文本文件, Files.newByteChannel()
该文件创建了一些SeekableByteChannel
连接到File
s 的实例。因此,它似乎可以防止文件在您调用时被即时删除 Files.delete(directoryToBeDeleted.toPath());
。
所以在删除文件之前关闭流,它应该可以工作。