我是 java 新手,我在根据用户输入的学生 ID 删除特定数据时遇到问题。我只能删除学生 ID,但不能删除有关该学生的所有信息。
这是我的代码:
File inputFile = new File("test.txt");
File tempfile = new File("temp.txt");
Scanner scanner = new Scanner("test.txt");
BufferedReader br = new BufferedReader(new FileReader("test.txt"));
PrintWriter wr = new PrintWriter("temp.txt");
String s = null;
String infoToRemove = JOptionPane.showInputDialog(null, "Enter ID to cancel");
while ((s = br.readLine()) != null) {
for (int i = 0; i < s.length(); i++) {
if (s.indexOf(infoToRemove) != -1) {
s = s.replaceAll(infoToRemove, "");
}
}
wr.println(s);
}
wr.close();
br.close();
inputFile.delete();
tempfile.renameTo(inputFile);
预期输出:
前:
jason,100,20,Male,Block 1,Ground Floor
jack,200,20,Male,Block 1,Ground Floor
用户插入ID=100
删除后:
jack,200,20,Male,Block 1,Ground Floor