Append text to a file in Java
This post will discuss how to append text at the end of a file in Java.
The text should be written at the end of the file rather than the beginning. If the file doesn’t exist, create a new one with the same name, and write data.
There are several ways to append text at the end of a file in plain Java and using third-party libraries such as Guava, Apache Commons IO, etc. All these are discussed below in detail:
1. Using Files.write() method
With the introduction of the Files class in Java 7, several static methods are included that operate on files, directories, or other types of files.
To write some text to a file, we can call Files.write(path, byte[], options) method. By default, the method creates a new file or overwrites an existing file. Since we need to append bytes to an existing file, pass the StandardOpenOption.APPEND option to it, as demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; class Main { public static void main(String[] args) { Path path = Paths.get("demo.txt"); String text = "…some text…"; try { Files.write(path, text.getBytes(), StandardOpenOption.APPEND); System.out.println("Successfully written bytes to the file"); } catch (IOException e) { e.printStackTrace(); } } } |
The above code throws java.nio.file.NoSuchFileException if target file doesn’t exist. To create a new file when target file doesn’t exist, additionally pass the StandardOpenOption.CREATE option to Files.write() method, as shown 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 |
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; class Main { public static void main(String[] args) { Path path = Paths.get("demo.txt"); String text = "…some text…"; try { Files.write(path, text.getBytes(), StandardOpenOption.APPEND, StandardOpenOption.CREATE ); System.out.println("Successfully written bytes to the file"); } catch (IOException e) { e.printStackTrace(); } } } |
2. Using FileWriter
We can even construct a FileWriter object for writing streams of characters into a file. FileWriter’s constructor takes the target file name with an optional boolean argument. The boolean argument, if true, indicates that bytes will be written at the end of the file rather than the beginning.
The following code directly calls the write() method, inherited from class java.io.Writer, to append a string in the file.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.io.FileWriter; import java.io.IOException; class Main { public static void main(String[] args) { String fileName = "demo.txt"; String text = "…some text…"; // `true` will append the new data try(FileWriter fw = new FileWriter(fileName, true)) { fw.write(text); System.out.println("Successfully written data to the file"); } catch (IOException e) { e.printStackTrace(); } } } |
3. Using Guava Library
Guava’s Files class has several utility methods for working with files. We can use its asCharSink() method with APPEND mode, which appends data at the end of the file without truncating it. When no mode is provided, the file will be truncated before writing, or a new file is created when the target file doesn’t exist.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import com.google.common.io.FileWriteMode; import com.google.common.io.Files; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; class Main { public static void main(String[] args) { File file = new File("demo.txt"); String text = "…some text…"; try { Files.asCharSink(file, StandardCharsets.UTF_8, FileWriteMode.APPEND) .write(text); System.out.println("Successfully written data to the file"); } catch (IOException e) { e.printStackTrace(); } } } |
4. Using Apache Commons IO
We can also use the FileUtils class from Apache Commons IO library that has the writeStringToFile(File, String, Charset, mode) method, which writes a string to a file. If the last parameter to this method is true, then the String will be added to the file’s end rather than overwriting. Like Guava’s Files.asCharSink() method, it creates the file if it does not exist.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; class Main { public static void main(String[] args) { File file = new File("demo.txt"); String text = "…some text…"; try { FileUtils.writeStringToFile(file, text, StandardCharsets.UTF_8); System.out.println("Successfully written data to the file"); } catch (IOException e) { e.printStackTrace(); } } } |
That’s all about appending text to a file in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)