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

Total Post:134

Posted on    August-14-2015 3:21 AM

 Java Java  File 

 1 Reply(s)
 506  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

Posted on    August-14-2015 8:12 AM

You can use fileWriter with a true for appending.

    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
catch(IOException ioe)
    System.err.println("IOException: " + ioe.getMessage());

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

Follow MindStick