将文件追加到不覆盖的文本文件中

Posted

技术标签:

【中文标题】将文件追加到不覆盖的文本文件中【英文标题】:appendFile into textfile that dont overwrite 【发布时间】:2016-01-01 07:43:18 【问题描述】:

我在 append 方法上失败了。

我有一个程序可以输入信息然后打印到文本文件中。

问题是,我也想将新的信息输入附加到文本文件中而不覆盖,这意味着如果我重新运行程序,我编码的信息将与最后一次运行的编码一起在文本文件中该程序。

请帮助我。这是程序:

@SuppressWarnings("resource")
public static void main(String[] args) throws IOException

   System.out.println("STUDENT INFORMATION");
   System.out.println("-------------------");
   System.out.println("Full Name            :   ");
    Scanner sc = new Scanner(System.in);
    String name = sc.nextLine();
   System.out.println("\nCourse and Year      :   ");
    String yearcourse = sc.nextLine();
   System.out.println("\nStudent Number       :   ");
    String studentnumber = sc.nextLine();
   System.out.println("\nE-mail Address       :   ");
    String eadd = sc.nextLine();
   System.out.println("\nCellphone Number     :   ");
    String cpnumber = sc.nextLine();
   System.out.println("\nGender               :   ");
    String gender = sc.nextLine();
   System.out.println("\nAge                  :   ");
    String age = sc.nextLine();
   System.out.println("\nDate of Birth        :   ");
    String dob = sc.nextLine();
   System.out.println("\nCivil Status         :   ");
    String status = sc.nextLine();
   System.out.println("\nAddress              :   ");
    String address = sc.nextLine();
   System.out.println("\n\n________________________________________________");
   System.out.println("STUDENT INFORMATION");
   System.out.println("\nName             :   " + name + "");
   System.out.println("Year and Course  :   " + yearcourse + "");
   System.out.println("Student Number   :   " + studentnumber + "");
   System.out.println("E-mail Address   :   " + eadd + "");
   System.out.println("Cellphone Number :   " + cpnumber + "");
   System.out.println("Gender           :   " + gender + "");
   System.out.println("Age              :   " + age + "");
   System.out.println("Date of Birth    :   " + dob + "");
   System.out.println("Civil Status     :   " + status + "");
   System.out.println("Address          :   " + address + "");
   System.out.println("");


    File file = new File("Student Information.txt");
    if(!file.exists())
    
        file.createNewFile();
    
    try (PrintWriter writer = new PrintWriter(file))
        BufferedWriter bufferWritter = new BufferedWriter(writer);
        writer.println("\nSTUDENT INFORMATION:");
        writer.println("--------------------");
        writer.println("Name             :   " + name + "");
        writer.println("Year and Course  :   " + yearcourse + "");
        writer.println("Student Number   :   " + studentnumber + "");
        writer.println("E-mail Address   :   " + eadd + "");
        writer.println("Cellphone Number :   " + cpnumber + "");
        writer.println("Gender           :   " + gender + "");
        writer.println("Age              :   " + age + "");
        writer.println("Date of Birth    :   " + dob + "");
        writer.println("Civil Status     :   " + status + "");
        writer.println("Address          :   " + address + "");
        writer.flush();
    

在这。如果我重新运行程序,文本文件将被新的编码信息覆盖。他们说要使用附加bufferwriter。但我真的迷路了。

【问题讨论】:

重新标记,更好的格式。 【参考方案1】:

试着把你的代码改成这样:

File file = new File("Student Information.txt");
    if(!file.exists())
    
        file.createNewFile();
    
    try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)))) 
        out.println("\nSTUDENT INFORMATION:");
        out.println("--------------------");
        out.println("Name             :   " + name + "");
        out.println("Year and Course  :   " + yearcourse + "");
        out.println("Student Number   :   " + studentnumber + "");
        out.println("E-mail Address   :   " + eadd + "");
        out.println("Cellphone Number :   " + cpnumber + "");
        out.println("Gender           :   " + gender + "");
        out.println("Age              :   " + age + "");
        out.println("Date of Birth    :   " + dob + "");
        out.println("Civil Status     :   " + status + "");
        out.println("Address          :   " + address + "");
catch (IOException e) 
    // Handle IO exception.

这部分代码:

new FileWriter(file,true)

告诉它在设置为 true 时追加,如您所见 on the docs。


或者你可以改变你的线路:

PrintWriter writer = new PrintWriter(file);

到这里:

PrintWriter writer = new PrintWriter(new FileWriter(file,true));

【讨论】:

【参考方案2】:

而不是这个

PrintWriter writer = new PrintWriter(file);

试试这个

PrintWriter writer= new PrintWriter(new BufferedWriter(new FileWriter(file, true));

另见

How to append content to file in Java How to append text to an existing file in Java

【讨论】:

以上是关于将文件追加到不覆盖的文本文件中的主要内容,如果未能解决你的问题,请参考以下文章

重定向,常用转义字符

C#往文件中追加文本内容信息

C#往文件中追加文本内容信息

python文本操作

如何从SharedPreferences文件中正确地将文本附加到TextView而不是覆盖它?

(shell脚本)特殊分隔符文本文件导入MongoDB