删除文本文件中的特定行
Remove specific line in text file
我有一个 .txt 文件,inventory.txt
。它包含
banana, 1, 15
dog, 1, 15
cats, 20, 30
我想创建一个方法,通过输入 cats
或 cats, 20, 30
来删除这些行之一,比方说 cats
行
我的代码提示用户输入 removedItem
,读取 inventory.txt
,修剪每一行,并检查 trimmedLine
是否等于 removedItem
,然后 [=22] =] 并写入 deleteditems.txt
每 line
读取不包括 trimmedLine
。然后我关闭 writer
和 reader
,删除原来的 inventory.txt
,并将 deleteditems.txt
重命名为 inventory.txt
。但是,它什么也没做,编译后该行仍然存在。
代码:
public void removeItems() throws IOException {
String line;
File inventory = new File("src/inventory.txt");
File temp = new File("src/deleteditems.txt");
BufferedReader reader = new BufferedReader(new FileReader("src/inventory.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("src/deleteditems.txt"));
displayInventory();
temp.createNewFile();
System.out.println("what item do you want to remove");
String removedLine = scan.next();
while((line = reader.readLine()) != null) {
String trimmedLine = line.trim();
if(trimmedLine.equals(removedLine)) {
trimmedLine = "";
}
writer.write(line + System.getProperty("line.separator"));
}
reader.close();
writer.close();
inventory.delete();
temp.renameTo(inventory);
}
输出:
banana, 1, 15
dog, 1, 15
cats, 20, 30
what item do you want to remove
cats, 20, 30
编译后的文本文件:
banana, 1, 15
dog, 1, 15
cats, 20, 30
您的操作存在两个问题:
- 您正在将整行与 "equals" 的输入进行比较....对于
为了匹配,用户不能只输入 "cats",他们需要
输入 "cats, 20, 30" 因为这是一行包含的内容。
- 即使匹配,您仍在将 "line" 写入输出文件
你可以这样修复:
while((line = reader.readLine()) != null) {
String trimmedLine = line.trim();
if(!trimmedLine.startsWith(removedLine)) {
writer.write(line +
System.getProperty("line.separator"));
}
}
这只会写入不以输入开头的行。
作为旁注,您应该考虑使用 "try with resources" 语句打开 reader/writer 以确保即使在发生异常时也能正确清理。
您正在尝试检查是否与 .equals()
完全匹配
尝试使用 .contains(CharSequence)
或 .startsWith()
String newInventory = "";
while((line = reader.readLine()) != null) {
String trimmedLine = line.trim();
if(!trimmedLine.contains(removedLine)) {
writer.write(line + System.getProperty("line.separator"));
continue;
}
newInventory += line;
}
FileOutputStream fout = new FileOutputSteam("src/inventory.txt");
fout.write(newInventory.getBytes());
我有一个 .txt 文件,inventory.txt
。它包含
banana, 1, 15
dog, 1, 15
cats, 20, 30
我想创建一个方法,通过输入 cats
或 cats, 20, 30
cats
行
我的代码提示用户输入 removedItem
,读取 inventory.txt
,修剪每一行,并检查 trimmedLine
是否等于 removedItem
,然后 [=22] =] 并写入 deleteditems.txt
每 line
读取不包括 trimmedLine
。然后我关闭 writer
和 reader
,删除原来的 inventory.txt
,并将 deleteditems.txt
重命名为 inventory.txt
。但是,它什么也没做,编译后该行仍然存在。
代码:
public void removeItems() throws IOException {
String line;
File inventory = new File("src/inventory.txt");
File temp = new File("src/deleteditems.txt");
BufferedReader reader = new BufferedReader(new FileReader("src/inventory.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("src/deleteditems.txt"));
displayInventory();
temp.createNewFile();
System.out.println("what item do you want to remove");
String removedLine = scan.next();
while((line = reader.readLine()) != null) {
String trimmedLine = line.trim();
if(trimmedLine.equals(removedLine)) {
trimmedLine = "";
}
writer.write(line + System.getProperty("line.separator"));
}
reader.close();
writer.close();
inventory.delete();
temp.renameTo(inventory);
}
输出:
banana, 1, 15
dog, 1, 15
cats, 20, 30
what item do you want to remove
cats, 20, 30
编译后的文本文件:
banana, 1, 15
dog, 1, 15
cats, 20, 30
您的操作存在两个问题:
- 您正在将整行与 "equals" 的输入进行比较....对于 为了匹配,用户不能只输入 "cats",他们需要 输入 "cats, 20, 30" 因为这是一行包含的内容。
- 即使匹配,您仍在将 "line" 写入输出文件
你可以这样修复:
while((line = reader.readLine()) != null) {
String trimmedLine = line.trim();
if(!trimmedLine.startsWith(removedLine)) {
writer.write(line +
System.getProperty("line.separator"));
}
}
这只会写入不以输入开头的行。
作为旁注,您应该考虑使用 "try with resources" 语句打开 reader/writer 以确保即使在发生异常时也能正确清理。
您正在尝试检查是否与 .equals()
完全匹配尝试使用 .contains(CharSequence) 或 .startsWith()
String newInventory = "";
while((line = reader.readLine()) != null) {
String trimmedLine = line.trim();
if(!trimmedLine.contains(removedLine)) {
writer.write(line + System.getProperty("line.separator"));
continue;
}
newInventory += line;
}
FileOutputStream fout = new FileOutputSteam("src/inventory.txt");
fout.write(newInventory.getBytes());