java删除文件中的行吗?

bsxbgnwa  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(309)

这个问题在这里已经有答案了

在文件中找到一行并删除它(16个答案)
使用bufferedreader/bufferedwriter删除文件的第n行(2个答案)
15天前关门了。
我有一个文本文件,其中包含重复的以下信息行(没有要点):
代码:12345
厕所。doe@gmail.com
10935710517038750
在每个“集合”中,数字和电子邮件地址都是不同的。这只是一个例子。我要做的是扫描文本文件,用我正在搜索的特定代码来确定行,然后删除代码、电子邮件和号码行。比如,包含代码的那一行以及接下来的两行。我一辈子都搞不懂怎么做。我学会了如何用别的东西替换这些行,但是我想完全删除它们,最好不必每次都创建一个全新的文本文件,除非有办法用删除的行创建新文本文件,并用这个新文件替换旧文件。以下是我的相关代码,以段为单位。代码将所有与oldline变量匹配的行替换为空行。这不是我想要的,但我想不出来。我从其他地方的一个例子中得到了大部分代码。

//Instantiating the File class
          String filePath = "C:\\\\Users\\\\taylo\\\\Astronomy\\\\Which Bright Stars Are Visible\\\\StoreVerificationCodes.txt";
          //Instantiating the Scanner class to read the file
          Scanner sc = new Scanner(new File(filePath));
          //instantiating the StringBuffer class
          StringBuffer buffer = new StringBuffer();
          //Reading lines of the file and appending them to StringBuffer
          while (sc.hasNextLine()) {
             buffer.append(sc.nextLine()+System.lineSeparator());
          }
          String fileContents = buffer.toString();
          System.out.println("Contents of the file: "+fileContents);
          //closing the Scanner object
          sc.close();
          String oldLine = "Code: 12345";
          String newLine = "";
          //Replacing the old line with new line
          fileContents = fileContents.replaceAll(oldLine, newLine);
          //instantiating the FileWriter class
          FileWriter writer = new FileWriter(filePath);
          System.out.println("");
          System.out.println("new data: "+fileContents);
          writer.append(fileContents);
          writer.flush();
          writer.close();
2g32fytz

2g32fytz1#

嗯,我想,你要找的是 substring 方法。我相信这是有原因的,为什么你有这个要求,你只有代码,你必须删除该集的下两行也。请看下面的代码。考虑到文件的结构是固定的,不会改变,它应该可以工作。

String filePath = "C:/Users/taylo/Astronomy/Which Bright Stars Are Visible/StoreVerificationCodes.txt";
        //Instantiating the Scanner class to read the file
        Scanner sc = new Scanner(new File(filePath));
        //instantiating the StringBuffer class
        StringBuffer buffer = new StringBuffer();
        //Reading lines of the file and appending them to StringBuffer
        while (sc.hasNextLine()) {
           buffer.append(sc.nextLine()+System.lineSeparator());
        }
        String fileContents = buffer.toString();
        System.out.println("Contents of the file: "+fileContents);
        //closing the Scanner object
        sc.close();
        String oldLine = "Code: 789678";
        String newLine = "";

       // My changes starts here.............
        String codePattern = "Code:";   // A fixed pattern
        int firstIndex = fileContents.indexOf(oldLine); // To get the index of code you looking for.
       int nextIndex= fileContents.indexOf(codePattern, firstIndex+1);
        if(nextIndex != -1) {
            nextIndex = fileContents.indexOf(codePattern, firstIndex+1) -5;
            fileContents = fileContents.substring(0, firstIndex) + fileContents.substring(nextIndex+3);
        }
        else
            fileContents = fileContents.substring(0, firstIndex);
         // My changes done here.............
        //fileContents = fileContents.replaceAll(oldLine, newLine);  //No need

        FileWriter writer = new FileWriter(filePath);
        System.out.println("");
        System.out.println("new data: "+fileContents);
        writer.append(fileContents);
        writer.flush();
        writer.close();

相关问题