【问题标题】:how read and delete line by line from a text file using java如何使用java从文本文件中逐行读取和删除
【发布时间】:2017-04-08 07:42:21
【问题描述】:

我有一个包含两行赞的文本文件

  • Hello World 1.
  • Hello World 2。

我想一一阅读并删除这 2 行。我成功地阅读了那 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 并重命名 tempFile 文件时出现问题。 这是我的输出

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

请任何人帮助我。提前致谢。

【问题讨论】:

    标签: java bufferedreader filereader printwriter


    【解决方案1】:

    问题是您无法删除removeLineFromFile 中的第一个文件,因为它仍然被您的buttonLineDeleteAction 方法锁定(因为您正在读取文件内容而您没有@987654323 @文件阅读器对象)。

    因此,您需要按照以下步骤操作:

    (1) 找出所有需要的行并将它们收集到一个列表中并关闭文件

    (2) 创建一个新文件并从List中写入内容并关闭文件

    (3) 立即删除旧文件

    (4)重命名新文件并关闭新文件

    【讨论】:

      猜你喜欢
      • 2011-08-17
      • 2020-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-13
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      相关资源
      最近更新 更多