Delete the Contents of a File in Java – 在Java中删除一个文件的内容

最后修改: 2019年 1月 23日

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

1. Introduction

1.绪论

In this tutorial, we’ll see how we use Java to delete the contents of a file without deleting the file itself. Since there are many simple ways to do it, let’s explore each one by one.

在本教程中,我们将看到如何使用Java来删除一个文件的内容,而不删除文件本身。由于有许多简单的方法,我们来逐一探讨。

2. Using PrintWriter

2.使用PrintWriter

Java’s PrintWriter class extends the Writer class. It prints the formatted representation of objects to the text-output stream.

Java的PrintWriter类扩展了Writer类。它将对象的格式化表示打印到文本输出流中。

We’ll perform a simple test. Let’s create a PrintWriter instance pointing to an existing file, deleting existing content of the file by just closing it, and then make sure the file length is empty:

我们将进行一个简单的测试。让我们创建一个指向现有文件的PrintWriter实例,通过直接关闭文件来删除文件的现有内容,然后确保文件长度为空。

new PrintWriter(FILE_PATH).close();
assertEquals(0, StreamUtils.getStringFromInputStream(new FileInputStream(FILE_PATH)).length());

Also, note that if we don’t need the PrintWriter object for further processing, this is the best option. However, if we need the PrintWriter object for further file operations, we can do this differently:

另外,请注意,如果我们不需要PrintWriter对象进行进一步处理,这是最好的选择。但是,如果我们需要PrintWriter对象进行进一步的文件操作,我们可以用不同的方法。

PrintWriter writer = new PrintWriter(FILE_PATH);
writer.print("");
// other operations
writer.close();

3. Using FileWriter

3.使用FileWriter

Java’s FileWriter is a standard Java IO API class that provides methods to write character-oriented data to a file.

Java的FileWriter是一个标准的Java IO API类,它提供了将面向字符的数据写到文件的方法。

Let’s now see how we can do the same operation using FileWriter:

现在让我们看看如何使用FileWriter:进行同样的操作。

new FileWriter(FILE_PATH, false).close();

Similarly, if we need the FileWriter object for further processing, we can assign it to a variable and update with an empty string.

同样,如果我们需要 FileWriter对象进行进一步的处理,我们可以把它分配给一个变量,然后用一个空字符串进行更新。

4. Using FileOutputStream

4.使用FileOutputStream

Java’s FileOutputStream is an output stream used for writing byte data to a file.

Java的FileOutputStream是一个输出流,用于将字节数据写到一个文件。

Now, let’s delete the content of the file using FileOutputStream:

现在,让我们使用FileOutputStream删除文件的内容:

new FileOutputStream(FILE_PATH).close();

5. Using Apache Commons IO FileUtils

5.使用Apache Commons IOFileUtils

Apache Commons IO is a library that contains utility classes to help out with common IO problems. We can delete the content of the file using one of its utility classes – FileUtils.

Apache Commons IO是一个包含实用类的库,可以帮助解决常见的IO问题。我们可以使用它的一个实用类–FileUtils.删除文件的内容。

To see how this works, let’s add the Apache Commons IO dependency to our pom.xml:

为了了解这一点,让我们将Apache Commons IO依赖性添加到我们的pom.xml

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>

After that, let’s take a quick example demonstrating deletion of file content:

之后,让我们举一个快速的例子,演示删除文件内容。

FileUtils.write(new File(FILE_PATH), "", Charset.defaultCharset());

6. Using Java NIO Files

6.使用Java NIOFiles

Java NIO File was introduced in JDK 7. It defines interfaces and classes to access files, file attributes, and file systems.

We can also delete the file contents using java.nio.file.Files:

我们也可以使用java.nio.file.Files删除文件内容。

BufferedWriter writer = Files.newBufferedWriter(Paths.get(FILE_PATH));
writer.write("");
writer.flush();

7. Using Java NIO FileChannel

7.使用Java NIOFileChannel

Java NIO FileChannel is NIO’s implementation to connect a file. It also complements the standard Java IO package.

Java NIO FileChannel是NIO的实现,用于连接一个文件。它也是对标准Java IO包的补充。

We can also delete the file contents using java.nio.channels.FileChannel:

我们还可以使用java.nio.channels.FileChannel删除文件内容。

FileChannel.open(Paths.get(FILE_PATH), StandardOpenOption.WRITE).truncate(0).close();

8. Using Guava

8.使用番石榴

Guava is an open source Java-based library that provides utility methods to do I/O operations. Let’s see how to use the Guava API for deleting the file contents.

Guava是一个基于Java的开源库,它提供了实用的方法来进行I/O操作。让我们看看如何使用Guava的API来删除文件内容。

First, we need to add the Guava dependency in our pom.xml:

首先,我们需要在我们的pom.xml中添加Guava依赖性。

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.0.1-jre</version>
</dependency>

After that, let’s see a quick example to delete file contents using Guava:

之后,让我们看看一个快速的例子,用Guava删除文件内容。

File file = new File(FILE_PATH);
byte[] empty = new byte[0];
com.google.common.io.Files.write(empty, file);

9. Conclusion

9.结语

To summarize, we’ve seen multiple ways to delete the content of a file without deleting the file itself.

总结一下,我们已经看到了多种删除文件内容而不删除文件本身的方法。

The full implementation of this tutorial can be found over on GitHub.

本教程的完整实现可以在GitHub上找到over