title: Java how to write to a file and append to it
body: <p>I can't get this program to write 4 lines of text to the same file javafile.txt. The first two sentences get overwritten by the next two sentences. I'm using BufferedWriter to accomplish this. I have tried so many different things but I keep overwriting the first sentences the javafile.txt ......................................................................................................................................................................................................................................................................................................................</p>
  
  <pre><code>import java.io.File;  
  import java.io.IOException;  
  import java.io.ObjectInputStream.GetField;
  import java.util.Scanner;
  import java.io.*; 
  import java.io.FileNotFoundException;  
  import java.io.BufferedReader;
  
  public class lab11_2 {
  
      public static void main(String[] args) {
  
          System.out.println("Practice writing to files \n");
  
  
  
      File Myfile = new File("C:\\Users\\test\\Desktop\\lab11\\javafile.txt");
  
          try {
  
                if (Myfile.createNewFile()) {
                  System.out.println("File created: " + Myfile.getName());
                } else {
                  System.out.println("File already exists.");
                }
              } catch (IOException e) {
                System.out.println("An error occurred.");
                e.printStackTrace();
              } //end create new file 
  
  
  
          try {
              System.out.println("Write to to file");
              BufferedWriter out = new BufferedWriter(new FileWriter("C:\\Users\\test\\Desktop\\lab11\\javafile.txt"));
               out.write("This is the first line of text in " + Myfile.getName() + "\n");
               out.close();
               out = new BufferedWriter(new FileWriter("C:\\Users\\test\\Desktop\\lab11\\javafile.txt",true));
               out.write("This is the second line of text in " + Myfile.getName() + "\n");
               out.close();
               System.out.println("Succesfully wrote to the the file");
               System.out.println();
  
            }
  
            catch (IOException e) {
               System.out.println("exception occoured"+ e);
            }
  
  
          if (Myfile.exists()) {
              System.out.println("Get file information:");
                System.out.println("File name: " + Myfile.getName());
                System.out.println("Absolute path: " + Myfile.getAbsolutePath());
                System.out.println("Writeable: " + Myfile.canWrite());
                System.out.println("Readable " + Myfile.canRead());
                System.out.println("File size in bytes " + Myfile.length());
              } else {
                System.out.println("The file does not exist.");
              }
  
  
          System.out.println();
          System.out.println("First read file");
           try {
  
                Scanner myReader = new Scanner(Myfile);
                while (myReader.hasNextLine()) {
                  String data = myReader.nextLine();
                  System.out.println(data);
                }
                myReader.close();
              } catch (FileNotFoundException e) {
                System.out.println("An error occurred.");
                e.printStackTrace();
              } // end try
  
  
  
           System.out.println();
           try {
                  System.out.println("Write to to file");
                   BufferedWriter out = new BufferedWriter(new FileWriter("C:\\Users\\test\\Desktop\\lab11\\javafile.txt"));
                   out.write("This is the Third line of text in " + Myfile.getName() + "\n");
                   out.close();
                   out = new BufferedWriter(new FileWriter("C:\\Users\\test\\Desktop\\lab11\\javafile.txt",true));
                   out.write("This is the fourth line of text in " + Myfile.getName() + "\n");
                   out.close();
  
  
                }
  
                catch (IOException e) {
                   System.out.println("exception occoured"+ e);
                }
  
           System.out.println("Done");
           System.out.println();
  
  
            }// end of main 
  
  
  }
  </code></pre>