How to Append Data to File in Java


In this example, let’s us see how to append content to a file in Java.

Source Code

ppackage com.beginner.examples;

import java.io.*;

public class FileOperator8Example { 
	public static void main(String[] args )
    {	
		try
        {
        	 File path = new File("example.txt");
        	 if (!path.exists()) 
        	 {
        		 path.createNewFile();
        	 }
        	 
             FileWriter writer = new FileWriter(path,true);
             BufferedWriter bw = new BufferedWriter(writer);  
             bw.write("rncontent");
             bw.flush();
             bw.close();
             System.out.println("Done");
        } 
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

Output:

Done
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments