简体   繁体   English

将appendFile转换为不会覆盖的文本文件

[英]appendFile into textfile that dont overwrite

Im loosing on append method. 我在append方法上失去了。

I have a program that inputs an information then printing in a text file. 我有一个输入信息然后在文本文件中打印的程序。

The problem is, I want to append the new input of information into text file too without overwriting, it means that if I rerun the program the information that I encoded will be in the textfile together with the encoded on the last run of the program. 问题是,我也想将新的信息输入也添加到文本文件中而不进行覆盖,这意味着如果我重新运行该程序,则我编码的信息将与该程序的最后一次运行时的编码一起存储在文本文件中。

Please help me. 请帮我。 Here is the program: 这是程序:

@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();
    }
}

} }

In this. 在此。 if I rerun the program, the text file will just overwrite with the new encoded information. 如果我重新运行该程序,则文本文件将被新的编码信息覆盖。 They said to use append bufferwriter . 他们说要使用append bufferwriter but I'm really lost. 但我真的迷路了

Try to change your code to something like this: 尝试将您的代码更改为如下所示:

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

This part of the code: 这部分代码:

new FileWriter(file,true)

tells it to append when it's set to true, as you can see on the docs . 告诉它在设置为true时追加,如您在docs上看到


Or you could change your line: 或者您可以更改行:

PrintWriter writer = new PrintWriter(file);

To this: 对此:

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

instead of this 代替这个

PrintWriter writer = new PrintWriter(file);

try this 尝试这个

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

See Also 也可以看看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM