There are several ways to append content to an existing file in Java. Below three ways to do it :

  1. Using FileWriter
  2. Using java.nio.file.Files
  3. Using Apache Commons IO FileUtils

1- Using FileWriter

JDK 1.4+

Since JDK 1.4, Java offers FileWriter to append content to an existing file. If the second argument of the FileWriter(File file, boolean append) constructor is set to  true, then bytes will be appended to the end of the file.

Below how to use it :

2- Using java.nio.file.Files

JDK 1.7+

Since Java 1.7, the static class java.nio.file.Files is given to operate on files and directories. It can be used to append content on file as below :

StandardOpenOption.APPEND is used to specify that bytes must be appended.

3- Using Apache Commons IO FileUtils

Since JDK 1.6 For Commons IO 2.4

Since JDK 1.5 For Commons IO 2.2

Appache Commons IO provides FileUtils to handle files and folders. You can use it to append content to an existing file.

  • Download the dependency

If you are working on a Maven project, add the following to your pom.xml

If not, download the jar and add it to your classpath.

  • Append content to file