Java Top

The early-bird price of the new Learn Spring Security OAuth course packages will increase by $50 on Wednesday:

>> CHECK OUT THE COURSE

1. Introduction

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.

2. Using PrintWriter

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

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:

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 writer = new PrintWriter(FILE_PATH);
writer.print("");
// other operations
writer.close();

3. Using FileWriter

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

Let's now see how we can do the same operation using 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.

4. Using FileOutputStream

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

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

new FileOutputStream(FILE_PATH).close();

5. Using Apache Commons IO FileUtils

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.

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

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</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

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:

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

7. Using Java NIO FileChannel

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

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

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

8. Using Guava

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.

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

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>22.0</version>
</dependency>

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

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

9. Conclusion

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.

Java bottom

The early-bird price of the new Learn Spring Security OAuth course packages will increase by $50 on Wednesday:

>> CHECK OUT THE COURSE
Comments are closed on this article!