我试图使用java从文本文件中删除一行,我的代码应该将文件写入临时文件并跳过写入用户名(选择删除),然后应该删除原始文件并重命名新的临时文件以匹配原始文件。以下是我的代码:
public static void deleteUser() throws FileNotFoundException, IOException {
System.out.println(
"Are you sure you want to delete a user? WARNING this will permanently remove them from our database\nmeaning you won't be able to recover their user from our index.txt file. (y/n)");
String answer = input.nextLine();
if (Objects.equals(answer, "y")) {
System.out.println("Please enter the exact username you would like to delete:");
String username = input.nextLine();
deleteUserFromFile(username, "index.txt", 1, " ");
System.out.println("User deleted.");
} else if (Objects.equals(answer, "n")) {
menu();
} else {
System.out.println("The answer provided can't be read. Please answer again using 'y' or 'n'.");
deleteUser();
}
}
public static void deleteUserFromFile(String username, String filepath, int positionOfTerm, String delimiter) {
String tempFile = "temp.txt";
File oldFile = new File(filepath);
File newFile = new File(tempFile);
String currentLine;
String data[];
try {
FileWriter fw = new FileWriter(tempFile, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
FileReader fr = new FileReader(filepath);
BufferedReader br = new BufferedReader(fr);
while ((currentLine = br.readLine()) != null) {
data = currentLine.split(delimiter);
if (!(data[positionOfTerm].equalsIgnoreCase(username))) {
pw.println(currentLine);
}
}
pw.flush();
pw.close();
fr.close();
br.close();
bw.close();
fw.close();
oldFile.delete();
File dump = new File(filepath);
newFile.renameTo(dump);
} catch (Exception e) {
System.out.println("An error occured.");
}
}
这段代码的捕获被触发了,我不知道为什么。非常感谢您的帮助。
作为参考,这是我的索引文件:
6
0 Gromit
1 Gwendolyn
2 Le-Spiderman
3 Wallace
4 Batman
5 Superman
编辑:
我已包括e.printStackTrace();正如所建议的,这是我从it收到的错误:
java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at Graph.deleteUserFromFile(Graph.java:239)
at Graph.deleteUser(Graph.java:208)
at Graph.menu(Graph.java:284)
at Graph.main(Graph.java:302)
以下是参考代码302:
menu();
284:
deleteUser();
208(此处的用户名是使用扫描仪输入收集的):
deleteUserFromFile(username, "index.txt", 1, " ");
239:
if (!(data[positionOfTerm].equalsIgnoreCase(username))) {
编辑2:
while ((currentLine = br.readLine()) != null) {
data = currentLine.split(delimiter);
String numOfUsernamesInList = br.readLine();
int newNumOfUsernamesInList = Integer.parseInt(numOfUsernamesInList) - 1;
pw.println(String.valueOf(newNumOfUsernamesInList));
if (data.length > positionOfTerm && !(data[positionOfTerm].equalsIgnoreCase(username))) {
pw.println(currentLine);
}
}
这是我尝试将索引文件中显示的6保留下来,并将其更新为5。当从索引文件中删除用户名时,错误消息如下:
java.lang.NumberFormatException: For input string: "0 Gromit"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at Graph.deleteUserFromFile(Graph.java:242)
at Graph.deleteUser(Graph.java:208)
at Graph.menu(Graph.java:290)
at Graph.main(Graph.java:308)