There are several ways to append content to an existing file in Java. Below three ways to do it :
Table of contents
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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.io.FileWriter; import java.io.IOException; import java.io.Writer; /** * Append content to file using FileWriter. * * @author Radouane ROUFID. * */ public class AppendToFileUsingFileWriter { public static void main(String[] args) { // File location. String filePath = "C:/dev/file.txt"; // Content to append. String contentToAppend = "\nThis line was added at the end of the file !"; // Set the second parameter of FileWriter to "true" to append to file. try (Writer fileWriter = new FileWriter(filePath, true)){ fileWriter.write(contentToAppend); } catch (IOException e) { System.out.println("Problem occurs when deleting the directory : " + filePath); e.printStackTrace(); } } } |
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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; /** * Append content to file using java.nio.file.Files. * * @author Radouane ROUFID. * */ public class AppendToFileUsingFiles { public static void main(String[] args) { // File location. String filePath = "C:/dev/file.txt"; // Content to append. String contentToAppend = "\nThis line was added at the end of the file !"; try { Files.write(Paths.get(filePath), contentToAppend.getBytes(), StandardOpenOption.APPEND); } catch (IOException e) { System.out.println("Problem occurs when deleting the directory : " + filePath); e.printStackTrace(); } } } |
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
1 2 3 4 5 |
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> |
If not, download the jar and add it to your classpath.
-
Append content to file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; /** * Append content to file using Apache Commons FileUtils. * * @author Radouane ROUFID. * */ public class AppendToFileUsingFileUtils { public static void main(String[] args) { // File location. String filePath = "C:/dev/file.txt"; File file = new File(filePath); // Content to append. String contentToAppend = "\nThis line was added at the end of the file !"; try { //Set the third parameter to true to specify you want to append to file. FileUtils.write(file, contentToAppend, true); } catch (IOException e) { System.out.println("Problem occurs when deleting the directory : " + filePath); e.printStackTrace(); } } } |