title: How to replace a file in Java
body: <p>I'm trying to rewrite the contents of a file by making a temporary file, manipulating the text, and then deleting the original to replace it with the temp. Here is the method:</p>
  
  <pre><code>private void deleteLine(String lineToRemove){
      File inputFile = new File("./src/class1.txt");
      File tempFile = new File("./src/tempFile.txt");
  
      BufferedReader reader;
      reader = new BufferedReader(new FileReader(inputFile));
  
      BufferedWriter writer;
      writer = new BufferedWriter(new FileWriter(tempFile));
  
      String currentLine;
  
      while((currentLine = reader.readLine()) != null) {
          if(!currentLine.trim().equals(lineToRemove)){
              writer.write(currentLine +     System.getProperty("line.separator"));
          }
      }
      writer.close();
      reader.close();
  
      System.out.println("Input to temp1: " + tempFile.renameTo(inputFile));
  }
  </code></pre>
  
  <p>When testing this method, I found that everything works as intended except for the last two lines, which both return false. My class1.txt file exists when the method starts, but tempFile.txt does not.</p>