将appendFile写入不会覆盖的文本文件中

时间:2015-10-04 15:41:13

标签: java append appendfile

我失去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。但我真的迷路了。

2 个答案:

答案 0 :(得分:0)

尝试将代码更改为以下内容:

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));

答案 1 :(得分:0)

而不是

PrintWriter writer = new PrintWriter(file);

试试这个

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

另见