HOW TO APPEND TEXT TO AN EXISTING FILE IN JAVA

Tanuj Kumar

Total Post:134

Points:940
Posted by  Tanuj Kumar
Java  File 
 789  View(s)
Ratings:
Rate this:
I need to append text repeatedly to an existing file in Java. How do I do that?
  1. Mayank Tripathi

    Post:397

    Points:3117
    Re: How to append text to an existing file in Java

    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());
    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!