How to Delete the Contents of a File

Learn to delete or clear the content of a file without deleting the file using standard IO classes and 3rd party libraries.

1. Using PrintWriter Constructor

The PrintWiter is used to write formatted strings to a text output stream.

The PrintWriter(file) constructor creates a new PrintWriter with the specified file parameter. If the file exists then it will be truncated to zero size; otherwise, a new file will be created.

File file = new File("/path/file");

try(PrintWriter pw = new PrintWriter(file)){
  //Any more operations if required
} catch (FileNotFoundException e) {
  e.printStackTrace();
}

2. Using FileWriter Constructor

The FileWeite is also used to write text to character files. Similar to PrintWriter, the constructor of the FileWriter also empties the file if the file is not been opened for the append operation.

In the given example, the second parameter false indicates the append mode. If it is true then bytes will be written to the end of the file rather than the beginning.

File file = new File("/path/file");

try(FileWriter fw = new FileWriter(file)){
  //Any more operations if required
} catch (IOException e) {
  e.printStackTrace();
}

3. Using RandomAccessFile

A random-access file behaves like a large array of bytes stored in the file system. We can use its setLength() method to empty the file.

try(RandomAccessFile raf = new RandomAccessFile(file, "rw")){
  raf.setLength(0);
} catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}

4. Using NIO’s Files.newBufferedWriter()

We can also use the BufferedWriter to write an empty string into the file. This will make the file size zero by deleting all the content of it.

try(BufferedWriter writer = Files.newBufferedWriter(file.toPath())){
  	writer.write("");
	writer.flush();
} catch (IOException e) {
  e.printStackTrace();
}

5. Using Commons IO FileUtils

The FileUtils class be used to write the empty string into the file that effectively deletes all the content present in the file.

File file = new File("/path/file");

try{
  FileUtils.write(file, "", StandardCharsets.UTF_8);
} catch (IOException e) {
  e.printStackTrace();
}

Include the latest version of Commons IO library from Maven.

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

6. Conclusion

In this Java tutorial, we learned to make the file empty by deleting all the content in it. This makes the file size zero without deleting the file itself.

We learned to use the Java IO’s PrintWriter, FileWriter, NIO’s Files class and Commons IO’s FileUtils class for emptying the file.

Happy Learning !!

Source Code on Github

Leave a Reply

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial