-1

I have a text file that will have repeated lines of the following information (without bullet points):

  • Code: 12345
  • john.doe@gmail.com
  • 10935710517038750

In each "set", the numbers would be different as well as the email address. This is just an example. What I want to do is scan through the text file, fine the line with the specific code I am searching for, then delete that code, email, and number line. Like, the line with the code in it as well as the next two lines. I cannot, for the life of me, figure out how to do this. I learned how to replace these lines with something else, but I would like to erase them completely, preferably without having to make a brand new text file every single time, unless there is a way to make the new text file with the deleted lines, and replace the old file with this new one. Here is the relevant code I have, in segments. The code replaces all lines matching the oldLine variable with an empty line. That isn't what I want, but I can't figure it out otherwise. I had gotten most of this code from an example elsewhere.

//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();
  • 1
    Write the data without the lines into a new file, after you're finished, delete the old file and rename the new one to the old file's name. – daniu Dec 25 '20 at 10:17
  • 2
    Does this answer your question? [Find a line in a file and remove it](https://stackoverflow.com/questions/1377279/find-a-line-in-a-file-and-remove-it) – Simon van Endern Dec 25 '20 at 10:21

1 Answers1

0

Well, I think, what you are looking for is substring method. I believe there would be some reason why you have this requirement where you just have the code and you have to delete the next two lines of that set also. Please have a look at below code. It should work, given that the structure of your file is fixed and not going to change.

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

Nish
  • 922
  • 13
  • 31
  • 1
    Thank you for the help! It works great, but it throws a string index out of range when I try to do the last code in the list, specifically on line fileContents = fileContents.substring..... – MusicalUniverse Dec 26 '20 at 02:47
  • Thanks @MusicalUniverse. That case I did not test. Have updated the solution. necessary extra checks you can put in place anytime. – Nish Dec 28 '20 at 17:32