在现有文件夹中,我使用以下方法创建了一个文件夹并在其中创建了多个文件:
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
方法首先删除它尝试删除的每个文件夹内的所有文件,然后才会删除该文件夹。该异常表明该文件夹内的每个文件的删除均已成功,因为如果没有成功,我会在尝试删除该文件夹的其中一个文件时早些时候收到异常。
我的问题是 - 为什么我会遇到该异常?
最佳答案
java.nio.file.Files.delete(Path path)
的 javadoc 很清楚:
If the file is a directory then the directory must be empty.
还指出:
This method can be used with the walkFileTree method to delete a directory and all entries in the directory, or an entire file-tree where required.
使用 Files.walkFileTree()
将使您的代码更清晰、更短,但请注意,它不会解决您的实际问题。
此外,删除所有资源的递归方法是正确的,因为您通过从较深的资源开始并备份较浅的资源来删除资源。
问题出在其他地方:实际上,您正在使用 Files.newByteChannel()
创建文本文件,该文件创建了一些连接到 File 的
。因此,当您调用 SeekableByteChannel
实例Files.delete(directoryToBeDeleted.toPath());
时,它似乎会阻止文件被动态删除。
因此,在删除文件之前关闭流,它应该可以工作。
关于java - 无法删除空文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50009312/