java - 无法删除空文件夹

标签 java windows java-8 filesystems

在现有文件夹中,我使用以下方法创建了一个文件夹并在其中创建了多个文件:

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/

相关文章:

windows - 使用 MSDeploy 到运行默认 IIS 的 Windows 7 计算机

c# - Windows 10 UAP - 编译数据绑定(bind)

java - 如何在 Oracle "Fix versions"和 Oracle JDK 版本之间进行转换?

java - JSOUP 未解析的编译 : ignoreHttpErrors() is undefined for the type Connection

从资源目录读取的 Java JKS 文件 keystore 异常

windows - Chrome 使用 Windows 8 模式

java - 通过使用 java 8 流 API 比较 2 个 map 来计算百分比?

java - 方法参数的目标类型转换,Lambda

Java,Double 数据类型有问题,一直返回 1.00 而不是 1.8

java - 如何在 Android 上使用代理进行 HTTP 连接?