Skip to content

Java Append to File with FileOutputStream

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

FileOutputStream is an output stream for writing data to a File or to a FileDescriptor. It takes an optional second parameter, which determines whether the data is appended to the file.

Java Append to File with FileOutputStream

This example appends data to a file with FileOutputStream.

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class JavaAppendFileFileOutputStream {
    
    public static void main(String[] args) throws FileNotFoundException, IOException {
        
        String fileName = "src/main/resources/sample.txt";
        
        byte[] tb = "Ramesh\n".getBytes();
        
        try (FileOutputStream fos = new FileOutputStream(fileName, true)) {
            fos.write(tb);
        }
    }
}
Clone this wiki locally