Java如何写入文件并附加到文件中

Java如何写入文件并附加到文件中,java,Java,我无法让这个程序将4行文本写入同一个文件javafile.txt。前两句话会被后两句话覆盖。我正在使用BufferedWriter来实现这一点。我尝试了很多不同的方法,但我一直在覆盖javafile.txt的第一句话徖 import java.io.File; import java.io.IOException; import java.io.ObjectInputStream.GetField; import java.util.Scanner; import java.io.*;

我无法让这个程序将4行文本写入同一个文件javafile.txt。前两句话会被后两句话覆盖。我正在使用BufferedWriter来实现这一点。我尝试了很多不同的方法,但我一直在覆盖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 


}

您可以打开一个新的
FileWriter
四次。其中两次,您将true作为构造函数参数传递,因此编写器处于append模式。这意味着它将添加到文件中,而不是覆盖它。其他的,你不使用附加模式,所以你的文件被覆盖

如果要附加到文件,请将第三个文件编写器更改为同时使用附加模式。否则,此时将覆盖包含前两句的文件

或者更好的是,不要打开和关闭文件四次。打开它一次,写下你想写的所有东西,然后在最后关闭它