by Dinesh Thakur

The constructor of FileOutputStream class is overload, when we pass Boolean argument (true) in the constructor of FileOutputStream, then the new text append in the file.

FileOutputStream file = new FileOutputStream ("Hello.txt", true);

Here is the Java Example for Append Text to File:

import java.io.*;

class AppendTexttoFile
{
          public static void main(String args[])throws IOException
       {
                 try
           {
                   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                   FileOutputStream file = new FileOutputStream("Hello.txt",true);
                   System.out.println("Enter the text and Type 'y' to Terminate");
                   char ch;
                   while((ch=(char)br.read())!='y')
                   file.write(ch);
                   file.close();
            }
                    catch(IOException e) {}
         }
}

Append Text to File Java Example