title: Is there a way to modify a file and read from the new file, which has the new content?
body: <p>When I delete a line from a text file, add it to a temporary text file, then replace the temporary file with the original, the scanner isn't loading the correct text. It is loading the content from the original file, not the updated file. I want it to load the updated file with the new content.</p>
  
  <pre><code>try
  {
      File inputFile = new File("department.txt");
      File tempFile = new File("departmentTemp.txt");
  
      BufferedReader reader = new BufferedReader(new FileReader(inputFile));
      BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
  
      String lineToRemove = departResIDField.getText();
      String currentLine;
  
      while((currentLine = reader.readLine()) != null) 
      {
           String trimmedLine = currentLine.trim();
           if(trimmedLine.startsWith(lineToRemove)) continue;
           writer.write(currentLine + System.getProperty("line.separator"));
      }
  
      writer.close(); 
      reader.close(); 
      inputFile.delete();
      tempFile.renameTo(inputFile);
  
      fWriter = new FileWriter(inputFile.getAbsoluteFile(), true);
      printOut = new PrintWriter(fWriter);
      writer = new BufferedWriter(fWriter);
      writer.write(departResIDField.getText() + "\t" + 
      departResNameField.getText() + "\r\n");
      writer.close();
  
      int result = Integer.parseInt(departResIDField.getText());
      depart.add(new Department(result, departResNameField.getText()));
  
      insResDepartModel.addElement(departResNameField.getText());
  
      depart.remove();
  
      //This is where I am having problem with the code
      //The scanner is scanning old data not the updated data             
      Scanner scan = new Scanner(inputFile);
  
      while(scan.hasNext())
      {
          depart.add(new Department(scan.nextInt(), scan.next()));
      }
      scan.close();
  
      for(int i = 0; i &lt; depart.getSize(); i++)
      {
          Department d1 = depart.get(i);
          System.out.println(d1.departID + d1.departName);
      }
  
      menuLayout.show(cardLayout, "15");
      departResIDField.setText(null);
      departResNameField.setText(null);
      departUpdateButton.setVisible(false);
      departCancelButton.setVisible(false);
  } catch (IOException f) 
      {
          f.printStackTrace();
      }
  </code></pre>
  
  <p>Output:
  1   Biology
  2   Chemistry
  4   Statistics
  5   Physics
  3   Math</p>
  
  <p>Expected Output:
  1   Biology
  2   Chemistry
  4   Statistics
  5   Physics
  3   Calculus</p>
  
  <p>Code removes "3    Math" and adds "3    Calculus</p>