这是我在文本文件中编辑记录的确切代码。
我的代码运行成功,但单击btnModify
后文本文件中的记录不会更新。我的代码出了什么问题?
P.s:这些代码用于我的awt程序。
btnModify.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ArrayList<String> tempData = new ArrayList<>();
String requestedID = txtID.getText().trim().toLowerCase();
String requestedName = txtName.getText().trim();
String requestedGender = txtGender.getText().trim();
String requestedContactNumber = txtContactNumber.getText().trim();
System.out.println("123");
try {
FileReader fr = new FileReader("test.txt")){
Scanner reader = new Scanner(fr);
System.out.println("123456");
while (reader.hasNext()){
String line = reader.nextLine();
String lineLtrim = line.toLowerCase().replaceAll("^\\{", "");
String lineRtrim = lineLtrim.replaceAll("\\}$", "");
String[] data = lineRtrim.split(",");
System.out.println("5566");
if(data[0].equals(requestedID)){
System.out.println("9999");
data[1] = data[1].replace(data[1], requestedName);
data[2] = data[2].replace(data[2], requestedGender);
data[3] = data[3].replace(data[3], requestedContactNumber);
tempData.add(data[0] + "," + data[1] + "," + data[2] + "," + data[3] );
outStream.write("{" + txtID.getText().replace(data[0], requestedID));
outStream.write("," + txtName.getText().replace(data[1], requestedName));
outStream.write("," + txtGender.getText().replace(data[2], requestedGender));
outStream.write("," + txtContactNumber.getText().replace(data[3], requestedContactNumber) + "}");
outStream.write("\r\n");
outStream.close();
System.out.println("632");
System.out.println(tempData);
JOptionPane.showMessageDialog(null, "Modify Successful!");
}
else {
tempData.add(data[0] + ":" + data[1] + ":" + data[2] + ":" + data[3]);
System.out.println("142");
System.out.println(tempData);
}
}
fr.close();
}
catch (Exception ex){
}
}
});
这是我的文本文件格式。
{001,Jack,male,0123456789}
{002,Mark,male,0198765432}