Java – Delete a File – Java – 删除一个文件

最后修改: 2014年 6月 28日

中文/混合/英文(键盘快捷键:t)

This quick article illustrates how to delete a File in Java – first using JDK 6, then JDK 7 and finally the Apache Commons IO library.

这篇快速文章说明了如何在Java中删除文件–首先使用JDK 6,然后是JDK 7,最后是Apache Commons IO库。

This article is part of the “Java – Back to Basic” series here on Baeldung.

本文是Baeldung网站上的 “Java – Back to Basic “系列的一部分。

1. With Java – JDK 6

1.使用Java – JDK 6

Let’s start with the standard Java 6 solution:

让我们从标准的Java 6解决方案开始。

@Test
public void givenUsingJDK6_whenDeletingAFile_thenCorrect() throws IOException {
    new File("src/test/resources/fileToDelete_jdk6.txt").createNewFile();

    File fileToDelete = new File("src/test/resources/fileToDelete_jdk6.txt");
    boolean success = fileToDelete.delete();

    assertTrue(success);
}

As you can see – the file must exist before the delete operation; if it doesn’t, the API will not throw any exceptions but will instead return false.

正如你所看到的–在删除操作之前,文件必须存在;如果不存在,API不会抛出任何异常,而是会返回false。

2. With Java – JDK 7

2.使用Java – JDK 7

Let’s move on to the JDK 7 solution:

让我们继续讨论JDK 7的解决方案。

@Test
public void givenUsingJDK7nio2_whenDeletingAFile_thenCorrect() throws IOException {
    Files.createFile(Paths.get("src/test/resources/fileToDelete_jdk7.txt"));

    Path fileToDeletePath = Paths.get("src/test/resources/fileToDelete_jdk7.txt");
    Files.delete(fileToDeletePath);
}

Now – this will make better use of exceptions. If the file doesn’t exist when the delete operation is triggered – an NoSuchFileException will be thrown by the API:

现在–这将更好地利用异常。如果在触发删除操作时文件不存在–API将抛出一个NoSuchFileException

java.nio.file.NoSuchFileException: srctestresourcesfileToDelete_jdk7.txt
    at s.n.f.WindowsException.translateToIOException(WindowsException.java:79)

3. With Commons IO

3.使用Commons IO

Commons IO allows us to control the exceptions behavior when deleting a File. For a quiet delete that swallows any possible exceptions:

Commons IO允许我们控制删除一个文件时的异常行为。对于一个安静的删除,吞噬了任何可能的异常。

@Test
public void givenUsingCommonsIo_whenDeletingAFileV1_thenCorrect() throws IOException {
    FileUtils.touch(new File("src/test/resources/fileToDelete_commonsIo.txt"));
    File fileToDelete = FileUtils.getFile("src/test/resources/fileToDelete_commonsIo.txt");
    boolean success = FileUtils.deleteQuietly(fileToDelete);

    assertTrue(success);
}

Note that we can still determine if the operation was successful or not by simply checking the return value of the delete method.

注意,我们仍然可以通过简单地检查删除方法的返回值来确定操作是否成功。

Now – if we do want an exception to be thrown:

现在–如果我们确实想抛出一个异常。

@Test
public void givenUsingCommonsIo_whenDeletingAFileV2_thenCorrect() throws IOException {
    FileUtils.touch(new File("src/test/resources/fileToDelete.txt"));

    FileUtils.forceDelete(FileUtils.getFile("src/test/resources/fileToDelete.txt"));
}

If the file to be deleted doesn’t exist on the filesystem, the API will throw a standard FileNotFoundException:

如果要删除的文件在文件系统中不存在,API将抛出一个标准的FileNotFoundException

java.io.FileNotFoundException: File does not exist: srctestresourcesfileToDelete.txt
    at org.apache.commons.io.FileUtils.forceDelete(FileUtils.java:2275)

And there you have it – 4 simple ways to delete a File in Java.

这就是在Java中删除文件的4种简单方法。