How to Append Text to an Existing File in Java?

How to Append Text to an Existing File in Java?

While working with Java, we might get a situation where we need to append some text or data to an existing file, such as updating records of transactions, updating the content of a blog, and updating the content of a file. Java provides multiple ways to append text or binary data to an existing file.

In this blog, we will learn multiple methods to append text to an existing file in Java.

Table of Contents:

Methods to Append Text to an Existing File in Java

Java provides various methods to append text to an existing file. Following are some of the methods for the same:

Method 1: Append Text to an Existing File using java.io Package

We can use the java.io package to append text to an existing file in Java. It provides multiple classes like FileWriter, BufferedWriter, and PrintWriter to append text to an existing file.

1.1 Using FileWriter

The FileWriter class allows writing characters to a file. By passing true as the second parameter in its constructor, we enable append mode.

Example:

example.txt – Before Appending:

Learn Java File Handling

Code:

Java

Output:

Using FileWriter Output

Explanation: In the above code, we have used

  • new FileWriter(filePath, true): The true enables append mode.
  • .write(): This method writes the new content at the end of the file.

example.txt – After Appending:

Learn Java File Handling 

Appending text using FileWriter.

1.2 Using BufferedWriter

BufferedWriter is a part of the java.io package and is used to efficiently write text data to a file. It is often preferred over FileWriter because it buffers the characters, reducing the number of write operations, which improves performance.

Example:

example.txt – Before Appending:

Java Append File Example  
Learn how to efficiently add text to a file in Java.

Code:

Java

Output:

Using BufferedWriter Output

Explanation: In the above code, BufferedWriter improves performance by reducing the number of direct write operations to the file

example.txt – After Appending:

Java Append File Example  
Learn how to efficiently add text to a file in Java.  
Appending text using BufferedWriter.

1.3 Using PrintWriter

PrintWriter is another useful class in the java.io package that allows writing formatted text to a file. It provides methods like println(), printf(), and print(), which make it easier to write text in an existing file.

Example:

example.txt – Before Appending:

Appending Data in Java Files

Code:

Java

Output:

Using PrintWriter Output

Explanation:

  • PrintWriter allows you to write formatted text to the file.
  • .println() adds a new line after each piece of content.

example.txt – After Appending:

Appending Data in Java Files  

Appending text using PrintWriter.

Method 2: Using FileOutputStream for Appending

FileOutputStream is part of the java.io package and is used to write binary or text data to a file. When used with the append mode, it makes sure that new data is added at the end of the file without overwriting the existing content.

Example:

example.txt – Before Appending:

Java File Append Mode Explained

Code:

Java

Output:

Using FileOutputStream for Appending Output

Explanation:

  • .write(data.getBytes()) converts the text into byte format.
  • true in the constructor enables append mode.

example.txt – After Appending:

Java File Append Mode Explained 

Appending text using FileOutputStream.

Method 3: Using java.nio.file for Appending

The java.nio.file package makes working with files easier and more efficient. You can use the Files.write() method with StandardOpenOption.APPEND to add text to an existing file without overwriting its content.

Example:

example.txt – Before Appending:

Effective File Handling in Java

Code:

Java

Output:

Using java.nio.file for Appending Output

Explanation:

  • Files.write() appends text to a file in a simpler and modern way.
  • StandardOpenOption.APPEND makes sure that data is added, not replaced.

example.txt – After Appending:

Effective File Handling in Java 

Appending text using java.nio.file.

Method 4: Using Third-Party Libraries

We can also append text to a file in Java using Guava and Apache Commons IO, which typically simplifies file operations while also improving efficiency.

4.1 Using Guava

Google Guava is a powerful third-party library that is used in Java file handling. The Files.append() method from Guava’s com.google.common.io.Files class provides an easy way to append text to an existing file.

Example:

example.txt – Before Appending:

Using Guava for Java File Operations

Code:

Java

Output:

Using Guava Output

example.txt – After Appending:

Using Guava for Java File Operations 

Appending text using Guava.

Note: Add the following dependency in your Maven project, to run the above code:

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

4.2 Using Apache Commons IO FileUtils

Apache Commons IO is a popular third-party library that simplifies file handling in Java. The FileUtils.writeStringToFile() method makes it easy to append text to an existing file.

Example:

example.txt – Before Appending:

Apache Commons IO for Java Files 

Code:

Java

Output:

Using Apache Commons IO FileUtils Output

example.txt – After Appending:

Apache Commons IO for Java Files   
This is new content to append.

Note: To run the above code, you may need to add the following dependencies in your Maven Project.

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

Conclusion

So far in this blog, we have learned how we can append text to an existing file in Java using multiple methods, such as using the java.io package, the FileOutputStream class, and java.nio.file package, Apache Commons IO, Guava’s com.google.common.io.Files. You can use any one of them according to the needs of your project.

If you want to learn more about Java, you may refer to our Java course.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner