Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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.15.1</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>31.0.1-jre</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 code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW