如何使用 java 从文本文件中逐行读取和删除
how read and delete line by line from a text file using java
我有一个包含 2 行赞的文本文件
- 你好世界 1.
- 你好世界 2.
我想逐行阅读并删除那两行。我成功地阅读了这两行,但是当我删除它们时,它失败了。这是我的代码。
@FXML
public void buttonLineDeleteAction(ActionEvent event) throws IOException {
try {
String line = null;
final File fileGpNumber = new File(filePath);
FileReader fileReader = new FileReader(fileGpNumber);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while ((line = bufferedReader.readLine()) != null) {
System.out.println("Line--- " + line);
boolean result = removeLineFromFile(line);
System.out.println("Result---> " + result);
}
} catch (IOException e) {
System.out.println("IOException " + e);
}
}
并通过此方法删除行。
private boolean removeLineFromFile(String lineToRemove) {
boolean isDeleted = false;
try {
File inFile = new File(filePath);
File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
BufferedReader br = new BufferedReader(new FileReader(filePath));
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
String line;
//Read from the original file and write to the new
//unless content matches data to be removed.
while ((line = br.readLine()) != null) {
if (!line.trim().equals(lineToRemove)) {
pw.println(line);
pw.flush();
isDeleted = false;
} else {
isDeleted = true;
}
}
pw.close();
br.close();
//Delete the original file
if (inFile.delete()) {
isDeleted = true;
} else {
isDeleted = false;
System.out.println("Could not delete file.");
}
//Rename the new file to the filename the original file had.
if (tempFile.renameTo(inFile)) {
isDeleted = true;
} else {
System.out.println("Could not rename file.");
isDeleted = false;
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return isDeleted;
}
现在删除我的 tempFile 中的那些行,但在删除 inFile 和重命名 thetempFile 文件时出现问题。
这是我的输出
Line--- Hello World 1.
Could not delete file.
Could not rename file.
Result---> false
Line--- Hello World 2.
Could not delete file.
Could not rename file.
Result---> false
请任何人帮助我。提前致谢。
问题是您无法删除 removeLineFromFile
中的第一个文件,因为它仍然被您的 buttonLineDeleteAction
方法锁定(因为您正在读取文件内容和你没有 close
文件 reader 对象)。
因此,您需要按照以下步骤操作:
(1)找出所有需要的行并将它们收集到一个列表中,然后关闭文件
(2)新建一个文件,写入List中的内容,关闭文件
(3) 立即删除旧文件
(4)重命名新文件并关闭新文件
我有一个包含 2 行赞的文本文件
- 你好世界 1.
- 你好世界 2.
我想逐行阅读并删除那两行。我成功地阅读了这两行,但是当我删除它们时,它失败了。这是我的代码。
@FXML
public void buttonLineDeleteAction(ActionEvent event) throws IOException {
try {
String line = null;
final File fileGpNumber = new File(filePath);
FileReader fileReader = new FileReader(fileGpNumber);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while ((line = bufferedReader.readLine()) != null) {
System.out.println("Line--- " + line);
boolean result = removeLineFromFile(line);
System.out.println("Result---> " + result);
}
} catch (IOException e) {
System.out.println("IOException " + e);
}
}
并通过此方法删除行。
private boolean removeLineFromFile(String lineToRemove) {
boolean isDeleted = false;
try {
File inFile = new File(filePath);
File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
BufferedReader br = new BufferedReader(new FileReader(filePath));
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
String line;
//Read from the original file and write to the new
//unless content matches data to be removed.
while ((line = br.readLine()) != null) {
if (!line.trim().equals(lineToRemove)) {
pw.println(line);
pw.flush();
isDeleted = false;
} else {
isDeleted = true;
}
}
pw.close();
br.close();
//Delete the original file
if (inFile.delete()) {
isDeleted = true;
} else {
isDeleted = false;
System.out.println("Could not delete file.");
}
//Rename the new file to the filename the original file had.
if (tempFile.renameTo(inFile)) {
isDeleted = true;
} else {
System.out.println("Could not rename file.");
isDeleted = false;
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return isDeleted;
}
现在删除我的 tempFile 中的那些行,但在删除 inFile 和重命名 thetempFile 文件时出现问题。 这是我的输出
Line--- Hello World 1.
Could not delete file.
Could not rename file.
Result---> false
Line--- Hello World 2.
Could not delete file.
Could not rename file.
Result---> false
请任何人帮助我。提前致谢。
问题是您无法删除 removeLineFromFile
中的第一个文件,因为它仍然被您的 buttonLineDeleteAction
方法锁定(因为您正在读取文件内容和你没有 close
文件 reader 对象)。
因此,您需要按照以下步骤操作:
(1)找出所有需要的行并将它们收集到一个列表中,然后关闭文件
(2)新建一个文件,写入List中的内容,关闭文件
(3) 立即删除旧文件
(4)重命名新文件并关闭新文件