Java FileWriter删除第一个输入
我是一个Java新手,我正在编写一个在线课堂应用程序,我需要在单击TakeAttention_sbm按钮时编写一个记录考勤的代码。当其中一人输入姓名进行考勤时,会将姓名保存到txt文件中,但当另一人输入姓名时,第一个姓名会被删除,不会显示 服务器的代码Java FileWriter删除第一个输入,java,swing,io,append,filewriter,Java,Swing,Io,Append,Filewriter,我是一个Java新手,我正在编写一个在线课堂应用程序,我需要在单击TakeAttention_sbm按钮时编写一个记录考勤的代码。当其中一人输入姓名进行考勤时,会将姓名保存到txt文件中,但当另一人输入姓名时,第一个姓名会被删除,不会显示 服务器的代码 if(e.getSource() == takeAttendance_sbm) { try { String input = JOptionPane.showInputDialog("Att
if(e.getSource() == takeAttendance_sbm) {
try {
String input = JOptionPane.showInputDialog("Attendance ");
System.out.println(input);
fw = new FileWriter(new File("mytextfile.txt"));
fw.write(input);
fw.write(System.lineSeparator());
fw.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
客户端的代码
if(e.getSource() == takeAttendance_sbm) {
move_flag = true;
try {
String input1 = JOptionPane.showInputDialog("Attendance ");
System.out.println(input1);
fw = new FileWriter(new File("mytextfile.txt"));
fw.write(input1);
fw.write(System.lineSeparator());
fw.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
您需要在追加模式下打开FileWriter,方法是将布尔文本true作为第二个参数传递给构造函数:
new FileWriter(new File("textfile.txt"), true);
这会将文本写入现有文件的结尾,而不是替换文件中原来的内容。您需要在追加模式下打开FileWriter,方法是将布尔文本true作为第二个参数传递给构造函数:
new FileWriter(new File("textfile.txt"), true);
这会将文本写入现有文件的末尾,而不是替换文件中原来的内容。如果站点允许,请执行。如果站点允许,请执行。