File.delete不会删除文件。

huangapple go评论90阅读模式
英文:

File.delete doesn't delete file

问题

在单元测试中,我在临时文件夹中创建了一个文件,在测试完成后,我想删除它们。

@After
public void destroy() throws IOException {
    if (!new File(tempFile.toAbsolutePath().getParent().toString() + "\\file1.tmp").delete()) {
        System.out.println("file1.tmp not deleted");
    }
    if (!new File(tempFile.toAbsolutePath().getParent().toString() + "\\file2.log").delete()) {
        System.out.println("file2.log not deleted");
    }
    if (!new File(tempFile.toAbsolutePath().getParent().toString() + "\\file3.log").delete()) {
        System.out.println("file3.log not deleted");
    }
    if (!new File(tempFile.toAbsolutePath().getParent().toString() + "\\file4.log").delete()) {
        System.out.println("file4.log not deleted");
    }
}

它可以成功删除文件1到文件3,但无法删除文件4。
通常情况下,File.delete应该会抛出一个IOException,但它只返回false。
我对每个文件都使用了相同的方法,在创建和写入文件后关闭了写入器。
我希望我没有忘记提供任何信息。

英文:

In a unit test I create a file in the temp folder, after the tests I want to delete them.

 @After
    public void destroy() throws IOException {
        if (!new File(tempFile.toAbsolutePath().getParent().toString() + "\\file1.tmp").delete()) {
            System.out.println("file1.tmp not deleted");
        }
        if (!new File(tempFile.toAbsolutePath().getParent().toString() + "\\file2.log").delete()) {
            System.out.println("file2.log not deleted");
        }
        if (!new File(tempFile.toAbsolutePath().getParent().toString() + "\\file3.log").delete()) {
            System.out.println("file3.log not deleted");
        }
        if (!new File(tempFile.toAbsolutePath().getParent().toString() + "\\file4.log").delete()) {
            System.out.println("file4.log not deleted");
        }
    }

It deletes the files 1-3 without a single problem but I doensn't delete file4.
Normally File.delete should throw an IOException but it just returns false.

I used the same method for each file, the writer is closed after creating and writing the files.

I hope I didn't forget any information

答案1

得分: 2

通常情况下,File.delete 应该抛出一个 IOException 异常,但它实际上只会返回 false

咳嗽...

如果 File.delete 在删除文件时失败,它不会抛出 IOException 异常<sup>1</sup>,而会返回 false

关于为什么删除文件失败的原因,最可能的解释是:

  • 文件被锁定,因为有其他程序正在使用。Windows 不允许删除被锁定的文件。

  • 应用程序没有足够的(操作系统级别的)权限来删除文件;也就是说,这是一个文件或目录权限问题。

如果你想找出删除失败的原因,解决方案是修改你的代码以使用 PathFiles.delete(Path)Files 中的方法被设计成会抛出异常。在这种情况下,异常消息应该会提供删除失败的原因。


<sup>1 - 该方法文档中唯一记录的异常是 SecurityException,只有在存在 SecurityManager 时才会抛出该异常。</sup>

英文:

> Normally File.delete should throw an IOException but it just returns false.

Ahem ...

File.delete does not throw an IOException if it fails to delete a file<sup>1</sup>. It returns false.

As to why it is failing to delete the file, the most likely explanations are:

  • The file is locked because something has it open. Windows won't let you delete a file that is locked.

  • The application doesn't have the required (OS level) access to delete the file due; i.e. it is a file or directory permission issue.

If you want to find out why the deletion is failing, the solution is to change your code to use Path and Files.delete(Path). The methods in Files are designed throw exceptions. In this case, the message should give the reason for the failure to delete.


<sup>1 - The only exception that the method is documented as throwing is SecurityException which will only be thrown if there is a SecurityManager active.</sup>

huangapple
  • 本文由 发表于 2020年8月7日 15:34:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63297260.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定