hey guys,
i have a little problem i have got a method that removes the line of text in a file.
for example if i specify a name and the name is on one of the lines in the file.txt it would remove it.
what i want is that once i specify the line it would remove that line and the following two for example i specify the name and it removes the surname and date of birth that are on the following two lines here is the method:

public void removeLineFromFile(String file, String lineToRemove) {

    try {

      File inFile = new File(file);

      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.getAbsolutePath() + ".tmp");

      BufferedReader br = new BufferedReader(new FileReader(file));
      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) {

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

any help would be greatly appreciated

thank you!!!

Dani AI

Generated

Quick note: had the right idea — after you detect the matching line, advance the BufferedReader two times to skip the following two lines. confirmed that approach works. The important extras are null checks (in case the file ends early), closing streams before deleting/renaming, and using a temp file in the same folder to avoid cross-filesystem rename problems.

A minimal, null-safe skip looks like this:

if (line.trim().equals(lineToRemove)) {
  // skip up to two following lines, stop early if EOF
  for (int i = 0; i < 2; i++) {
    if (br.readLine() == null) break;
  }
} else {
  pw.println(line);
}

Practical tips and cautions:

  • Use try-with-resources for the reader/writer so streams are always closed. Closing before deleting/renaming is required on Windows.
  • Put the temp file in the same directory as the original to allow an atomic rename on the same filesystem.
  • Avoid calling flush() after every println(); closing the writer flushes automatically and is faster.
  • If you only want to remove the first matching group, exit the loop (break) after skipping. If you want all occurrences, keep looping.
  • Consider matching rules: equals, equalsIgnoreCase, prefix checks, or regex depending on how strict the match must be.
  • For large or concurrent environments, consider file locking or use NIO Files.move(...) with REPLACE_EXISTING (and beware that ATOMIC_MOVE may not be supported on all platforms).

Troubleshooting: if delete/rename fails, verify streams are closed, check file permissions and other processes holding the file, and ensure the temp file is in the same directory as the original.

Recommended Answers

All 3 Replies

On line 28, put an else and read the next 2 lines from the input file.

ok , im not too sure how the code would look like, could you give me an example
thank you very much for your responce

it worked thanks a lot thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.