Pages

How to append text to existing File in Java? Example

In the last tutorial, you have learned about how to write data to a file in Java and in this tutorial you will learn how to append text to a file in Java. What is the difference between simply writing to a file vs appending data to a file? In the case of writing to a file, a program can start writing from the start but in the case of appending text, you start writing from the end of the file. You can append text into an existing file in Java by opening a file using FileWriter class in append mode. You can do this by using special constructor provided by FileWriter class, which accepts a file and a boolean, which if passed as true then open the file in append mode. This means you can write new content at the end of the file. One of the common examples of appending text to file is logging but for that you don't need to write your own logger, there are several good logging library available in Java world e.g. Log4j, SLF4j, Logbak and even java.util.logging is good enough.
In this tutorial, you will learn how to append data to an existing text file from Java program. As I said previously, if you are starting fresh in Java then I suggest you to better follow a book because they provide comprehensive coverage, which means you can learn a lot of things in quick time.

You can follow either core Java by Cay S. Horstmann or Java : A Beginners guide by Herbert Schildt, both are a very good book and highly recommended for beginners in Java. Good point about a beginner's guide is that it also covers Java 8 while core Java 9th Edition only covers up to Java 7.

Java Program to append text to existing File

Here is our complete Java example to demonstrate how to append text to a file in Java. This program shows an example using both Java SE 6 code and Java SE 7 code by using new feature try-with-resource statement to automatically close the resource.

In both examples, the key point to understand is opening the file using FileWriter in append mode. There is a special constructor for that which accepts a boolean argument to open the file in append mode.

FileWriter is a class from java.io package to write data into file one character at a time, as we have seen earlier, it's better to wrap FileWriter inside BufferedWriter and PrintWriter for efficient and convenient writing into the file.

In this program, we have a file called names.txt, which is at the root of the classpath, inside project directory of Eclipse IDE. This file contains two names at the beginning and after we run our program next set of names will be appended to it e.g. added at the end of the file. Since we are doing append two times, first by using Java 6 code and second by using Java SE 7 code, you will see a couple of names appended to file.

From the output, it's clear that our program is working as expected. In the first example, Shane is added just next to Hobert because there was no new line there. Later since we have used PrintWriter, newline characters e.g. \n are automatically appended after each line.

Steps to append text to a file In Java 6

Open the file you want to append text using FileWriter in append mode by passing true

Wrap FileWriter into BufferedReader if you are going to write large text

Wrap PrintWriter if you want to write in new line each time

Close FileWriter in finally block to avoid leaking file descriptors

Steps to append data into existing file In Java 7

Use try-with-resource statement to open resources e.g. FileWriter, BufferedWriter, and PrintWriter

Just write content using the println() method of PrintWriter.

The resource will be closed automatically when control will leave the try block. If there is any exception thrown from try block then that will be suppressed.

That's all about how to append text into existing file in Java. You can use FileWriter to open the file for appending text as opposed to writing text. The difference between them is that when you append data, it will be added at the and of the file. Since FileWriter writes one character at a time, it's better to use BufferedWriter class for efficient writing. You can also use PrintWriter if you want to use its convenient print() and println() method for writing lines into the file but it's not necessary to write text at the end of the file.