java - 为什么开始和结束字符串之间的删除在 Java 中不能正常工作?

标签 java

我有以下代表书籍的文件

Book: ISBN=3
title=english1
publishDate=1/12/2015
pageCount=200
authors=[12, 11, john]
-------------------------
Book: ISBN=5
title=english2
publishDate=1/12/2015
pageCount=200
authors=[12, 11, john2]
-------------------------
Book: ISBN=6
title=english3
publishDate=1/12/2015
pageCount=200
authors=[12, 11, john3]
-------------------------

我尝试使用此函数从文件中删除第二本书: 函数:

public static void removeLineFromFile(String file, String start, String end) {
    try {

      File inFile = new File(file);

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

      BufferedReader br = new BufferedReader(new FileReader(file));
      PrintWriter pw = new PrintWriter(new FileWriter(tempFile));    
      String line = null;
      Boolean flag=true;
      //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(start)) {
         flag=false;  

        }
        if(line.trim().equals(end)){
         flag=true; 
        }

        if (flag && !(line.trim().equals(end))){
        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();
    }
}

函数调用:

public static void main(String[] args) {
       removeLineFromFile("E:\\file.txt", "Book: ISBN=5","-------------------------")
}

这本书被删除了,但其他的“------------------------”分隔符也被删除了 `

Book: ISBN=3
title=english1
publishDate=1/12/2015
pageCount=200
authors=[12, 11, john]
Book: ISBN=6
title=english3
publishDate=1/12/2015
pageCount=200
authors=[12, 11, john3]

我应该怎么做才能得到以下结果:

Book: ISBN=3
title=english1
publishDate=1/12/2015
pageCount=200
authors=[12, 11, john]
-------------------------
Book: ISBN=6
title=english3
publishDate=1/12/2015
pageCount=200
authors=[12, 11, john3]
-------------------------

最佳答案

这是我能想到的最简单的解决方案:

public static void removeLineFromFile(String file, String start, String end) {
    try {

      File inFile = new File(file);

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

      BufferedReader br = new BufferedReader(new FileReader(file));
      PrintWriter pw = new PrintWriter(new FileWriter(tempFile));    
      String line = null;
      Boolean flag=true;
      //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(start)) {
         flag=false;  
        }

        if (flag){
        pw.println(line);
        pw.flush();
        }

        if(line.trim().equals(end)){
             flag=true; 
             continue; 
            }

      }
      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();
    }
}

问题是你太早设置了标志,因此没有打印出这条线。

关于java - 为什么开始和结束字符串之间的删除在 Java 中不能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34157060/

相关文章:

java - 重新排列数组 - java

java - 在这种情况下,我该如何纠正功能嫉妒?

java - 使用 REST/JPA 发送可选附加属性的最佳方式是什么?

java - 文字字符串创建与 String 对象创建

java - Android 如何从 Parse 列表中删除选定的项目

java - 在java中用double返回一个对象?

java - 用于用户身份验证的 JDBC 无法与 gui 一起使用,即使输入正确的用户名和密码也会阻止访问

Java - 知道在 attr 中包含我当前对象的 obj

java - 如何使用 freemarker 模板获取 url?

java - 如何调用Spring Security Provider?