how-to-append-text-to-an-existing-file-in-java?
[bsa_pro_ad_space id=1]

While utilizing Java, we may encounter circumstances where it is necessary to supplement some text or information to an already existing file, such as modifying transaction records, refreshing the content of a blog, or altering the content of a file. Java offers several methods to add text or binary data to a pre-existing file.

In this article, we will explore various techniques to append text to an existing file in Java.

Contents Overview:

Ways to Append Text to an Existing File in Java

Java offers a variety of methods to add text to an already existing file. Below are some of these methods:

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

We can utilize the java.io package for appending text to an existing file in Java. It provides several classes such as FileWriter, BufferedWriter, and PrintWriter for appending text to an existing file.

1.1 Employing FileWriter

The FileWriter class permits writing characters to a file. By specifying true as the second argument in its constructor, we activate append mode.

Example:

example.txt – Prior to Appending:

Learn Java File Handling

Code:

Java

Code Copied!

Output:

“`html
class=”aligncenter size-full”>Using FileWriter Output

Description: In the preceding code snippet, we utilized

  • new FileWriter(filePath, true): The parameter true activates append mode.
  • .write(): This function appends new content at the file’s conclusion.

example.txt – Subsequent to Appending:

Learn Java File Management 

Appending text utilizing FileWriter.

1.2 Employing BufferedWriter

BufferedWriter belongs to the java.io package and is designed for effectively writing textual data to a file. It is typically favored over FileWriter as it buffers characters, decreasing the number of write operations, thus enhancing efficiency.

Illustration:

example.txt – Prior to Appending:

Java Append File Sample  
Discover how to effectively include text in a file using Java.

Code:

Java

Code Duplicated!

Result:

Using BufferedWriter Output

Description: Within the code above, BufferedWriter enhances efficiency by minimizing the frequency of direct write actions to the file.

example.txt – Following Appending:

Java Append File Sample  
Discover how to effectively include text in a file using Java.  
Appending text utilizing BufferedWriter.

1.3 Utilizing PrintWriter

PrintWriter serves as another beneficial class within the java.io package that facilitates the writing of formatted text into a file. It offers functions such as println(), printf(), and print(), simplifying the process of writing text in an already existing file.

Illustration:

example.txt – Prior to Appending:

Appending Data in Java Files

Code:

Java

Code Duplicated!

Output:

Using PrintWriter Output

Clarification:

  • PrintWriter enables you to write formatted text to a file.
  • .println() introduces a new line following each segment of content.

example.txt – Following Appending:

Appending Data in Java Files  

Adding text using PrintWriter.

Method 2: Utilizing FileOutputStream for Appending

FileOutputStream is a component of the java.io package and is designed for writing binary or text data to a file. When engaged in append mode, it guarantees that new data is incorporated at the file's end without erasing the pre-existing content.

Illustration:

example.txt – Preceding Appending:

Java File Append Mode Clarified

Code:

Java

Code Duplicated!

Result:

Using FileOutputStream for Appending Output

Clarification:

  • .write(data.getBytes()) transforms the text into byte format.
  • true as an argument permits append mode.

example.txt – Post Appending:

Java File Append Mode Explained 

Incorporating text using FileOutputStream.

Method 3: Utilizing java.nio.file for Appending

The java.nio.file package simplifies file management, making it more efficient. You can leverage the Files.write() function with StandardOpenOption.APPEND to insert text into an existing file without overwriting its existing content.

Instance:

example.txt – Prior to Appending:

Effective File Management in Java

Code:

Java

Code Copied!

Result:

Using java.nio.file for Appending Output

Clarification:

  • Files.write() attaches text to a file in a more straightforward and contemporary manner.
  • StandardOpenOption.APPEND ensures that information is added instead of overwritten.

example.txt ```– Following Append Operation:

Efficient File Management in Java 

Adding text via java.nio.file.

Method 4: Utilizing External Libraries

It is also possible to add text to a file in Java by employing Guava and Apache Commons IO, which generally enhances file management while also boosting performance.

4.1 Utilizing Guava

Google Guava is a robust external library for Java file management. The Files.append() method from the Guava’s com.google.common.io.Files class offers a simple approach to add text to an existing file.

Example:

example.txt – Prior to Append:

Employing Guava for Java File Functions

Code:

Java

Code Copied!

Output:

Using Guava Output

example.txt – Following Append Operation:

Employing Guava for Java File Functions 

Adding text via Guava.

Note: Please include the following dependency in your Maven project in order to execute the above code:

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

4.2 Utilizing Apache Commons IO FileUtils

Apache Commons IO is a well-known external library that facilitates file management in Java. The FileUtils.writeStringToFile() method allows easy appending of text to an existing file.

Example:

example.txt – Prior to Append:

Apache Commons IO for Java Files 

Code:

Java

Code Copied!

Output:

Using Apache Commons IO FileUtils Output

example.txt – Subsequent Appending:

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

Remark: To execute the above code, you might need to include the following dependencies in your Maven Project.

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

Summary

In this blog, we have explored how to append text to an existing file in Java using various techniques, including the java.io package, the FileOutputStream class, and the java.nio.file package, along with Apache Commons IO and Guava’s com.google.common.io.Files. You can choose any of these methods based on your project requirements.

If you wish to delve deeper into Java, you may refer to our Java course.

The article How to Append Text to an Existing File in Java? was first published on Intellipaat Blog.

```