Pass true as a second argument to FileWriter to turn on "append" mode.

fout = new FileWriter("filename.txt", true);

FileWriter usage reference

Answer from Amber on Stack Overflow
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java io โ€บ java โ€“ append data to a file
Java โ€“ Append Data to a File | Baeldung
January 16, 2024 - Now, letโ€™s see how we can start using Guava to append content to an existing file: @Test public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException { File file = new File(fileName); CharSink chs = Files.asCharSink( file, Charsets.UTF_8, FileWriteMode.APPEND); chs.write("Spain\r\n"); assertThat(StreamUtils.getStringFromInputStream( new FileInputStream(fileName))) .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); }
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ java-append-to-file
Java append to file | DigitalOcean
August 3, 2022 - File file = new File("append.txt"); FileWriter fr = new FileWriter(file, true); BufferedWriter br = new BufferedWriter(fr); br.write("data"); br.close(); fr.close(); We can also use PrintWriter to append to file in java.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2014 โ€บ 01 โ€บ how-to-append-to-a-file-in-java
How to append to a file in java using BufferedWriter, PrintWriter
Using this you can easily format the content which is to be appended to the File. import java.io.File; import java.io.FileWriter; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.IOException; class AppendFileDemo2 { public static void main( String[] args ) { try{ File file =new File("C://myfile.txt"); if(!file.exists()){ file.createNewFile(); } FileWriter fw = new FileWriter(file,true); BufferedWriter bw = new BufferedWriter(fw); PrintWriter pw = new PrintWriter(bw); //This will add a new line to the file content pw.println(""); /* Below three statements would add three * mentioned Strings to the file in new lines.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-append-a-string-in-an-existing-file
Java Program to Append a String in an Existing File - GeeksforGeeks
July 11, 2025 - It Constructs a FileWriter object given a File object in append mode. Now let us toggle onto methods of this class which is invoked here and play a crucial role in appending a string in an existing file as follows: ... This method closes the ...
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2012 โ€บ 12 โ€บ how-to-append-text-into-file-in-java-filewriter-example.html
How to append text into File in Java โ€“ FileWriter Example
Java program to append text into existing file in Java using FileWriter class. When you want to add text to an already existing file, you can use this utility.
๐ŸŒ
Centron
centron.de โ€บ startseite โ€บ java append to file โ€“ tutorial
Appending Data to a File in Java โ€“ Best Methods
February 18, 2025 - ... If you are working on text data and the number of write operations is less, use FileWriter and use its constructor with append flag value as true. If the number of write operations is huge, you should use the BufferedWriter.
๐ŸŒ
Alvin Alexander
alvinalexander.com โ€บ java โ€บ how-to-append-text-file-filewriter-bufferedwriter
Java: How to append text to a file with the FileWriter class | alvinalexander.com
March 19, 2024 - Here's a sample mini-application that appends data to the end of the checkbook.dat data file. Pay attention to the way the FileWriter constructor is invoked: // JavaFileAppendFileWriterExample.java // Created by https://alvinalexander.com import java.io.*; public class JavaFileAppendFileWriterExample { public static void main (String[] args) { JavaFileAppendFileWriterExample a = new JavaFileAppendFileWriterExample(); a.appendToCheckbook(); } // end main public void appendToCheckbook () { BufferedWriter bw = null; try { // -------------------- // APPEND MODE SET HERE // -------------------- bw
Find elsewhere
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ i/o โ€บ appending to a file in java
Appending to a File in Java
April 22, 2022 - FileOutputStream is meant for writing ... FileWriter. To append content to an existing file, open FileOutputStream in append mode by passing the second argument as true....
๐ŸŒ
ZetCode
zetcode.com โ€บ java โ€บ appendfile
Java append to file - learn how to append to file in Java
FileWriter takes an optional second parameter: append. If set to true, then the data will be written to the end of the file. ... import java.io.FileWriter; import java.io.IOException; import java.nio.charset.StandardCharsets; void main() throws IOException { String fileName = "src/main/res...
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 637179 โ€บ java โ€บ Appending-Existing-File-Overwriting
Appending to an Existing File instead of Overwriting It [Solved] (I/O and Streams forum at Coderanch)
Kevin Simonson wrote:But when I ... Is there a way to call the constructor so that any future writes to it simply append to the original contents, instead of overwriting them? Yes, FileWriter has a constructor which you can pass a filename and a boolean value in as a second ...
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ io โ€บ FileWriter.html
FileWriter (Java Platform SE 8 )
April 21, 2026 - append - boolean if true, then data will be written to the end of the file rather than the beginning. ... IOException - if the named file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason ...
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ examples โ€บ append-text-existing-file
Java Program to Append Text to an Existing File
In our case, we used APPEND option for writing. Since the write() method may return an IOException, we use a try-catch block to catch the exception properly. import java.io.FileWriter; import java.io.IOException; public class AppendFile { public static void main(String[] args) { String path = System.getProperty("user.dir") + "\\src\\test.txt"; String text = "Added text"; try { FileWriter fw = new FileWriter(path, true); fw.write(text); fw.close(); } catch(IOException e) { } } }
๐ŸŒ
Java67
java67.com โ€บ 2015 โ€บ 07 โ€บ how-to-append-text-to-existing-file-in-java-example.html
How to append text to existing File in Java? Example | Java67
You can do this by using a 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.
Top answer
1 of 16
929

Are you doing this for logging purposes? If so there are several libraries for this. Two of the most popular are Log4j and Logback.

Java 7+

For a one-time task, the Files class makes this easy:

try {
    Files.write(Paths.get("myfile.txt"), "the text".getBytes(), StandardOpenOption.APPEND);
}catch (IOException e) {
    //exception handling left as an exercise for the reader
}

Careful: The above approach will throw a NoSuchFileException if the file does not already exist. It also does not append a newline automatically (which you often want when appending to a text file). Another approach is to pass both CREATE and APPEND options, which will create the file first if it doesn't already exist:

private void write(final String s) throws IOException {
    Files.writeString(
        Path.of(System.getProperty("java.io.tmpdir"), "filename.txt"),
        s + System.lineSeparator(),
        StandardOpenOption.CREATE, StandardOpenOption.APPEND
    );
}

However, if you will be writing to the same file many times, the above snippets must open and close the file on the disk many times, which is a slow operation. In this case, a BufferedWriter is faster:

try(FileWriter fw = new FileWriter("myfile.txt", true);
    BufferedWriter bw = new BufferedWriter(fw);
    PrintWriter out = new PrintWriter(bw))
{
    out.println("the text");
    //more code
    out.println("more text");
    //more code
} catch (IOException e) {
    //exception handling left as an exercise for the reader
}

Notes:

  • The second parameter to the FileWriter constructor will tell it to append to the file, rather than writing a new file. (If the file does not exist, it will be created.)
  • Using a BufferedWriter is recommended for an expensive writer (such as FileWriter).
  • Using a PrintWriter gives you access to println syntax that you're probably used to from System.out.
  • But the BufferedWriter and PrintWriter wrappers are not strictly necessary.

Older Java

try {
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("myfile.txt", true)));
    out.println("the text");
    out.close();
} catch (IOException e) {
    //exception handling left as an exercise for the reader
}

Exception Handling

If you need robust exception handling for older Java, it gets very verbose:

FileWriter fw = null;
BufferedWriter bw = null;
PrintWriter out = null;
try {
    fw = new FileWriter("myfile.txt", true);
    bw = new BufferedWriter(fw);
    out = new PrintWriter(bw);
    out.println("the text");
    out.close();
} catch (IOException e) {
    //exception handling left as an exercise for the reader
}
finally {
    try {
        if(out != null)
            out.close();
    } catch (IOException e) {
        //exception handling left as an exercise for the reader
    }
    try {
        if(bw != null)
            bw.close();
    } catch (IOException e) {
        //exception handling left as an exercise for the reader
    }
    try {
        if(fw != null)
            fw.close();
    } catch (IOException e) {
        //exception handling left as an exercise for the reader
    }
}
2 of 16
203

You can use fileWriter with a flag set to true , for appending.

try
{
    String filename= "MyFile.txt";
    FileWriter fw = new FileWriter(filename,true); //the true will append the new data
    fw.write("add a line\n");//appends the string to the file
    fw.close();
}
catch(IOException ioe)
{
    System.err.println("IOException: " + ioe.getMessage());
}
๐ŸŒ
Jenkov
jenkov.com โ€บ tutorials โ€บ java-io โ€บ filewriter.html
Java FileWriter
FileWriter has a constructor that takes 2 parameters too: The file name and a boolean. The boolean indicates whether to append or overwrite an existing file. Here are two Java FileWriter examples showing that:
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ examples โ€บ append-text-to-an-existing-file
Java Program to Append Text to an Existing File | Vultr Docs
December 9, 2024 - Use Files.write(), offering a simpler syntax for appending text using NIO. Specify file path, text to append, and the append option in standard open options. ... import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; ...
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 7 โ€บ docs โ€บ api โ€บ java โ€บ io โ€บ FileWriter.html
FileWriter (Java Platform SE 7 )
append - boolean if true, then data will be written to the end of the file rather than the beginning. ... IOException - if the named file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason ...
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ java-filewriter-example
Java FileWriter Example | DigitalOcean
August 4, 2022 - FileWriter(File file, boolean append): Creates a FileWriter object using specified File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning.