每次有人在我的java应用程序上注册时,我都会尝试升级到一个新的信息

每次有人在我的java应用程序上注册时,我都会尝试升级到一个新的信息,java,file-writing,Java,File Writing,所以每当有人在我的程序上单击“注册”,我就会调用一个方法来打开文件并添加记录。 这是为了打开文件: try { l = new Formatter("chineses.txt"); System.out.println("Did create"); } catch (Exception e){ System.out.println("Did not create"); } public void addRecord(){ //This is how i add the

所以每当有人在我的程序上单击“注册”,我就会调用一个方法来打开文件并添加记录。 这是为了打开文件:

try {
    l = new Formatter("chineses.txt");
    System.out.println("Did create");
} catch (Exception e){
    System.out.println("Did not create");
}

public void addRecord(){ //This is how i add the record
    l.format("%s", nameField.getText());
}
每次我在“名称”字段中输入名称并在gui中单击“注册”时,它总是替换文本文件第一行中的内容。
我如何让它转到第二行,同时保留第一行的内容,每次其他人输入他们的姓名并单击“注册”?

您只需要创建FileWriter对象并将其传递给格式化程序。它会将您的文本附加到文件中

请尝试以下代码:

FileWriter writer = new FileWriter("chineses.txt",true);
l = new Formatter(writer);

public void addRecord(){ //This is how i add the record
    l.format("%s\n", nameField.getText());
}