Hi. I am trying to delete a particular line from a text file but I am having a bit of trouble. My plan is to read the contents of the file, writing it to a new file at the same time, but omitting the line that needs to be deleted. Next the plan was to delete the old file and rename the new file to the old file name...

Java Code:
public static void removeLine(String file, String lineToRemove) {
 
    try {
 
      File inFile = new File(file);
      File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
 
      BufferedReader br = new BufferedReader(new FileReader(file));
      PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
 
      String line = null;
 
      while ((line = br.readLine()) != null) {
 
        if (!line.trim().equals(lineToRemove)) {
 
          pw.println(line);
          pw.flush();
        }
      }
      pw.close();
      br.close();
 
      if (!inFile.delete()) {
        System.out.println("Could not delete file");
        return;
      }
 
      if (!tempFile.renameTo(inFile))
        System.out.println("Could not rename file");
 
    }
    catch (Exception e) {
      System.out.println(e);
    }
  }

It does not seem to work though. The file in question is being run elsewhere in the system at the same time. Could that be the problem and if so, any suggestions on how to resolve this?