gpt4 book ai didi

java - 从文本文件中删除特定行

转载 作者:行者123 更新时间:2023-11-30 10:33:46 25 4
gpt4 key购买 nike

我试图从文件中删除特定行。但是我在从文本文件中删除特定行时遇到问题。比方说,我想在以下文件中删除 Blueberry 的文本文件:

旧列表文本文件:

Chocolate
Strawberry
Blueberry
Mango

新列表文本文件:

Chocolate
Strawberry
Mango

我尝试运行我的 Java 程序,当我输入删除时,它没有从文本文件中删除该行。

输出:请删除:d蓝莓删除:蓝莓

当我打开我的文本文件时,它只循环显示“Blueberry”这个词。

文本文件:

Blueberry
Blueberry
Blueberry
Blueberry
Blueberry
Blueberry
Blueberry
Blueberry

我的问题是如何从文本文件中删除特定行?

这是我的 Java 代码:

String input="Please delete: ";
System.out.println(input);

try
{
BufferedReader reader = new BufferedReader
(new InputStreamReader (System.in));
line = reader.readLine();

String inFile="list.txt";
String line = "";


while(!line.equals("x"))
{
switch(line)
{

case "d":

line = reader.readLine();
System.out.println("Remove: " + line);
String lineToRemove="";

FileWriter removeLine=new FileWriter(inFile);
BufferedWriter change=new BufferedWriter(removeLine);
PrintWriter replace=new PrintWriter(change);

while (line != null) {
if (!line.trim().equals(lineToRemove))
{
replace.println(line);
replace.flush();
}
}
replace.close();
change.close();
break;

}
System.out.println(input);
line = reader.readLine();


}

}
catch(Exception e){
System.out.println("Error!");
}

最佳答案

让我们快速看一下您的代码...

line = reader.readLine();
//...
while (line != null) {
if (!line.trim().equals(lineToRemove))
{
replace.println(line);
replace.flush();
}
}

基本上,您读取文件的第一行,然后反复将其与 lineToRemove 进行比较,直到永远。这个循环永远不会退出

这是一个概念验证,您需要根据需要对其进行修改。

基本上,您需要确保自己正在做的是,您正在读取输入文件的每一行,直到没有更多行为止

// All the important information
String inputFileName = "...";
String outputFileName = "...";
String lineToRemove = "...";
// The traps any possible read/write exceptions which might occur
try {
File inputFile = new File(inputFileName);
File outputFile = new File(outputFileName);
// Open the reader/writer, this ensure that's encapsulated
// in a try-with-resource block, automatically closing
// the resources regardless of how the block exists
try (BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {
// Read each line from the reader and compare it with
// with the line to remove and write if required
String line = null;
while ((line = reader.readLine()) != null) {
if (!line.equals(lineToRemove)) {
writer.write(line);
writer.newLine();
}
}
}

// This is some magic, because of the compounding try blocks
// this section will only be called if the above try block
// exited without throwing an exception, so we're now safe
// to update the input file

// If you want two files at the end of his process, don't do
// this, this assumes you want to update and replace the
// original file

// Delete the original file, you might consider renaming it
// to some backup file
if (inputFile.delete()) {
// Rename the output file to the input file
if (!outputFile.renameTo(inputFile)) {
throw new IOException("Could not rename " + outputFileName + " to " + inputFileName);
}
} else {
throw new IOException("Could not delete original input file " + inputFileName);
}
} catch (IOException ex) {
// Handle any exceptions
ex.printStackTrace();
}

看看Basic I/OThe try-with-resources Statement了解更多详情

关于java - 从文本文件中删除特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42035929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com