0

How to delete a line from a text file java? I searched everywhere and even though I can't find a way to delete a line. I have the text file: a.txt

 1, Anaa, 23
 4, Mary, 3

and the function taken from internet:

public void removeLineFromFile(Long id){
   try{
       File inputFile = new File(fileName);
       File tempFile = new File("C:\\Users\\...myTempFile.txt");

       BufferedReader reader = new BufferedReader(new FileReader(inputFile));
       BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

       String lineToRemove = Objects.toString(id,null);
       String currentLine;

       while((currentLine = reader.readLine()) != null) {
           // trim newline when comparing with lineToRemove
           String trimmedLine = currentLine.trim();
           String trimmLine[] = trimmedLine.split(" ");
           if(!trimmLine.equals(lineToRemove)) {
               writer.write(currentLine + System.getProperty("line.separator"));
           }
       }
       writer.close();
       reader.close();
       boolean successful = tempFile.renameTo(inputFile);
   }catch (IOException e){
       e.printStackTrace();
   }

}

where the fileName is the path for a.txt. I have to delete the line enetering the id.That's why I split the trimmedLine. At the end of execution I have 2 files, the a.txt and myTempFile both having the same lines(the ones from beginning). Why couldn't delete it?

1 Answers1

1

If I understand your question correctly, you want to delete the line whose id matches with the id passed in the removeLineFromFile method.

To make your code work, only few changes are needed.

To extract the id, you need to split using both " " and ","

i.e.

           String trimmLine[] = trimmedLine.split(" |,");

where | is the regex OR operator. See Java: use split() with multiple delimiters.

Also, trimmLine is an array, you can't just compare trimmLine with lineToRemove. You first need to extract the first part which is the id from trimmLine. I would suggest you to look at the working of split method if you have difficulty in understanding this. You can have a look at How to split a string in Java.

So, extract the id which is the first index of the array trimmLine here using:

           String part1 = trimmLine[0];

and then compare part1 with lineToRemove.

Whole code looks like:

public void removeLineFromFile(Long id){
   try{
      File inputFile = new File(fileName);
      File tempFile = new File("C:\\Users\\...myTempFile.txt");

      BufferedReader reader = new BufferedReader(new FileReader(inputFile));
      BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

      String lineToRemove = Objects.toString(id,null);
      String currentLine;

      while((currentLine = reader.readLine()) != null) {
          // trim newline when comparing with lineToRemove
          String trimmedLine = currentLine.trim();
          String trimmLine[] = trimmedLine.split(" |,");
          String part1 = trimmLine[0];

          if(!part1.equals(lineToRemove)) {
              writer.write(currentLine + System.getProperty("line.separator"));
          }
      }
      writer.close();
      reader.close();
      boolean successful = tempFile.renameTo(inputFile);
   }catch (IOException e){
   e.printStackTrace();
 }
}
Community
  • 1
  • 1
Racing
  • 66
  • 1
  • 11