Skip to content

Java Append to File with FileWriter

Ramesh Fadatare edited this page Jul 12, 2019 · 1 revision

FileWriter class is used for writing streams of characters. FileWriter takes an optional second parameter: append. If set to true, then the data will be written to the end of the file.

Java Append to File with FileWriter

This example appends data to a file with FileWriter.

import java.io.FileWriter;
import java.io.IOException;

public class JavaAppendFileFileWriter {
    
    public static void main(String[] args) throws IOException {
        
        String fileName = "src/main/resources/sample.txt";
        
        try (FileWriter fw = new FileWriter(fileName, true)) {
            
            fw.append("Ramesh\n");
        }
    }
}
Clone this wiki locally