title: How do I remove the last line of a text file in Java?
body: <p>I have a file that includes a few strings.
  For example:</p>
  
  <p>Mike1111<br>
  Chris2222<br>
  Brian3333</p>
  
  <p>I am trying to write code to delete a line from that file. However, when I delete Chris for example, it becomes:</p>
  
  <p>Mike1111<br>
  Brian3333<br>
  (blank line)</p>
  
  <p>The blank line is a problem because when I add another string it will come after the blank line rather than in it's place. Thank you in advance. Here is my code:</p>
  
  <pre><code>public void deleteUser(String u, String p) throws IOException {
      username = u;
      pin = p;
  
      File userAccountFolder = new File("userLogin.txt");
      File temporaryFile = new File("temporaryFile.txt");
  
      BufferedReader buffReader = new BufferedReader(new FileReader(userAccountFolder));
      BufferedWriter buffWriter = new BufferedWriter(new FileWriter(temporaryFile));
  
      String lineToRemoveFromFile = username + pin;
      String lineFromUserFile;
  
      while ((lineFromUserFile = buffReader.readLine()) != null) {
  
          String trimmedLine = lineFromUserFile.trim();
          if (trimmedLine.equals(lineToRemoveFromFile)) {
            continue;
          }
  
          buffWriter.append(lineFromUserFile + System.getProperty("line.separator"));
      }
      buffWriter.close();
      buffReader.close();
  
      boolean removed = temporaryFile.renameTo(userAccountFolder);
      if (removed == true) {
          System.out.println(username + "'s account has been removed.");
  
      }
  
  }
  </code></pre>