title: Java how to remove one line in a file?
body: <p>I want to delete one line of record in a File using Java, example in my file, </p>
  
  <pre>
  studentID  studentName studentAddress studentPhoneNo
  AAA|AAA AAA | AAAAAAAAAAA | AAAAAAAAAAAA
  BBB|BBB BBB | BBBBBBBBBBB | BBBBBBBBBBBB
  CCC|CCC CCC | CCCCCCCCCCC | CCCCCCCCCCCC
  </pre>
  
  <p>Let said I want to delete studentID= BBB below are the code I found from google :</p>
  
  <pre><code>try {
  
          File inFile = new File(studentFile);
  
          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(studentFile));
          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().contains(id)) {
  
                  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();
      }
  </code></pre>
  
  <p>But I got this error Could not delete file.</p>