首页 > 解决方案 > 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.*; 
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 


}

标签: java

解决方案


请参阅FileWriter api

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

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

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


推荐阅读