Home > DeveloperSection > Forums > How to append text to an existing file in Java
Tanuj Kumar

Total Post:134

Points:940
Posted on    August-14-2015 3:21 AM

 Java Java  File 
Ratings:


 1 Reply(s)
 499  View(s)
Rate this:
I need to append text repeatedly to an existing file in Java. How do I do that?


Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Posted on    August-14-2015 8:12 AM

You can use fileWriter with a 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());
}

Don't want to miss updates? Please click the below button!

Follow MindStick