-1

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 ......................................................................................................................................................................................................................................................................................................................

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 


}

1 Answers1

1

See the FileWriter api.

You open a new FileWriter four times. Two of those times, you pass true as a constructor parameter, so the writer is in append mode. That means it adds to the file instead of overwriting it. The others, you don't use append mode, so your file is overwritten.

If you want to append to a file, change the third file writer to also use append mode. Otherwise at that point you overwrite the file containing the first two sentences.

Or better yet, don't open and close the file four times. Open it once and write everything you want to write, then close it at the end.

khelwood
  • 55,782
  • 14
  • 81
  • 108