title: When is my this file being locked?
body: <p>I have the following method:</p>
  
  <pre><code>  public static void main(String[] args)
    {
        DataWriter.addScore(2, DataWriter.score(8.5,2.25, 170.0);
     }
  </code></pre>
  
  <p>And this class, DataWriter:</p>
  
  <pre><code>   public class DataWriter
      {
              public static int findTextInFile(String s) //returns an int representing the line number of String s
              {
                  try
                  {
                  int ln = 0;
                  File data = new File("data.txt");
  
                      String line = null;
                      Scanner fileInput = new Scanner(data);
                      while(fileInput.hasNextLine())
                      {
                      String prevLine = fileInput.nextLine();
                      ln++;
                      if(prevLine.equals(s))
                      {
                       //   br.close(); 
                          return ln;
                      }
  
                  }
                 //br.close();
              }
              catch(Exception e) { System.out.println(e); } 
  
              return 0;
  
          }
          public static void addScore(int prompt, String stats) //Calls the WriteAfterNthLine method with a prompt number and stats string.
              {
                  try
                  {
                      switch(prompt)
                      { 
                          case 1:
                              writeAfterNthLine("data.txt", stats, findTextInFile("NORMAL"));
                              break;
                          case 2:
                              writeAfterNthLine("data.txt", stats, findTextInFile("SMALL"));
                              break;
                          case 3:
                              writeAfterNthLine("data.txt", stats, findTextInFile("NUMBERS"));
                              break;
                          case 4:
                              writeAfterNthLine("data.txt", stats, findTextInFile("COMMON WORDS"));
                              break;
                          case 5:
                              writeAfterNthLine("data.txt", stats, findTextInFile("OTHER"));
                              break;
                          default:
                              System.out.println("ERROR INVALID SUBMISSION"); 
                          break;
                  }
                  }
                  catch(Exception e)
                  {
                      System.out.println(e);
                  }
              }
     public static void writeAfterNthLine(String filename, String text, int lineno) throws IOException //Writes String to filename on line lineno
          {
          //TODO:Document this method.
              File file = new File(filename); 
              File tmpDir = new File("C:/Users/jacobw4107/Documents/EZType"); //This is the current directory.
              System.out.println("Made temporary Directory " +tmpDir);
              File temp = File.createTempFile("temp-file-name", ".tmp", tmpDir);
              BufferedReader br = new BufferedReader(new FileReader( file ));
              PrintWriter pw =  new PrintWriter(new FileWriter( temp ));
              String line;
              int lineCount = 0;
              while ((line = br.readLine()) != null) {
                  pw.println(line);
                  System.out.println(line);
                  if(lineCount==lineno){
                      pw.println(text);
                  }
                  lineCount++;
              }
              br.close();
              pw.close();
              File old = new File("data.old");
              if(old.exists())
                  System.out.println(old.delete()); 
              System.out.println(file.renameTo(new File("data.old")));
              System.out.println(temp.renameTo(new File("data.txt")));
      }
             }
  </code></pre>
  
  <p>When I call the <em>writeAfterNthLine</em> method by itself, everything functions correctly. However, Running the main method above causes the final two lines of the <em>writeAfterNthLine</em> method to fail. The final two and <em>renameto</em> lines fail, and return false. If I try to manually rename <em>data.txt</em>, I am given the following error:</p>
  
  <blockquote>
    <p>The action can't be completed because the file is open in Java(TM) Platform SE binary.</p>
  </blockquote>
  
  <p><strong>So which method is locking the file?</strong></p>
  
  <p>I am running Windows 10 Education version 1703. I do <strong>not</strong> have administrator privileges.</p>