How to append a string in an existing file?


Levels of difficulty: / perform operation:

Problem & Solution

This example shows how to append a string in an existing file using filewriter method.

JAVA Program

import java.io.*;

public class Main {
   public static void main(String[] args) throws Exception {
      try {
         BufferedWriter out = new BufferedWriter
         (new FileWriter("filename"));
         out.write("aString1\n");
         out.close();
         out = new BufferedWriter(new FileWriter
         ("filename",true));
         out.write("aString2");
         out.close();
         BufferedReader in = new BufferedReader
         (new FileReader("filename"));
         String str;
         while ((str = in.readLine()) != null) {
            System.out.println(str);
         }
      }
      in.close();
      catch (IOException e) {
         System.out.println("exception occoured"+ e);
      }
   }
}

Output

The above code sample will produce the following result.

aString1
aString2