繁体   English   中英

JAVA使用关键字从.txt文件中删除一行

[英]JAVA removing a line from a .txt file with a keyword

try {

      File inFile = new File("books.txt");

      if (!inFile.isFile()) {
        System.out.println("Parameter is not an existing file");
        return;
      }

      //Construct the new file that will later be renamed to the original filename. 
      File tempFile = new File(inFile + "temp.txt");

      BufferedReader br = new BufferedReader(new FileReader("books.txt"));
      PrintWriter pw = new PrintWriter(new FileWriter(tempFile));

      String line = null;

      //Read from the original file and write to the new 
      //unless content matches data to be removed.
      while ((line = br.readLine()) != null) {

        String lineToRemove;
        lineToRemove = JOptionPane.showInputDialog("Enter line to remove");
        if (!line.trim().contains(lineToRemove)) {

          pw.println(line);
          pw.flush();
        }
      }
      pw.close();
      br.close();

      //Delete the original file
      if (!inFile.delete()) {
        System.out.println("Could not delete file");
        return;
      } 

      //Rename the new file to the filename the original file had.
      if (!tempFile.renameTo(inFile))
        System.out.println("Could not rename file");

    }
    catch (FileNotFoundException ex) {
      ex.printStackTrace();
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
  }

我正在尝试创建一种方法,如果我输入该行中包含的单词,则会删除.txt文件中的一行代码。 我的问题是,运行此命令时,测试表明无法删除该文件。 代码有问题吗? 我的代码也用#分隔,如果可能会加重问题。 示例输出为:Giants#James#1993

我已经对您的代码进行了细微的测试(在我撰写本文时,@ Andreas注意到了这一点),并且该代码可以正常工作

    public static void main(String[] args) {
    try {

          File inFile = new File("C:\\rirubio\\books.txt");

          if (!inFile.isFile()) {
            System.out.println("Parameter is not an existing file");
            return;
          }

          //Construct the new file that will later be renamed to the original filename. 
          File tempFile = new File("C:\\rirubio\\books.txt" + "_temp");

          BufferedReader br = new BufferedReader(new FileReader(inFile));
          PrintWriter pw = new PrintWriter(new FileWriter(tempFile));

          String line = null;

          String lineToRemove = JOptionPane.showInputDialog("Enter line to remove");

          //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().contains(lineToRemove)) {
              pw.println(line);
              pw.flush();
            }
          }

          pw.close();
          br.close();

          //Delete the original file
          if (!inFile.delete()) {
            System.out.println("Could not delete file");
            return;
          } 

          //Rename the new file to the filename the original file had.
          if (!tempFile.renameTo(inFile)) {
            System.out.println("Could not rename file");
          }

        } catch (FileNotFoundException ex) {
          ex.printStackTrace();
        } catch (IOException ex) {
          ex.printStackTrace();
        }
      }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM