尝试使用 java 从 .txt 文件中删除一行时收到异常和代码失败
Receiving an exception and code failing when trying to delete a line from a .txt file using java
我正在尝试使用 java 从我的文本文件中删除特定行,我的代码应该将文件写入临时文件并跳过写入用户名(选择删除),它是然后应该删除原始文件并重命名新的临时文件以匹配原始文件。以下是我的代码:
// remove a user
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();
}
}
// remove user function
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();按照建议,这是我从中收到的错误:
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(); // updating the username number counter at the top of the
// index file
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)
第一行的data[positionOfTerm]
文件不存在。您需要先检查数组长度。
if (data.length > positionOfTerm && !(data[positionOfTerm].equalsIgnoreCase(username))) {
我正在尝试使用 java 从我的文本文件中删除特定行,我的代码应该将文件写入临时文件并跳过写入用户名(选择删除),它是然后应该删除原始文件并重命名新的临时文件以匹配原始文件。以下是我的代码:
// remove a user
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();
}
}
// remove user function
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();按照建议,这是我从中收到的错误:
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(); // updating the username number counter at the top of the
// index file
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)
data[positionOfTerm]
文件不存在。您需要先检查数组长度。
if (data.length > positionOfTerm && !(data[positionOfTerm].equalsIgnoreCase(username))) {