title: file.renameTo(newFile) not working in Java
body: <p>I have seen this question many times but I have not found a solution that works for me. I am trying to rename the file to a new file name. I am getting false every time and am not sure why like most others with the same problem. </p>
  
  <p>my code is as follows:</p>
  
  <pre><code>File file = new File("filePath.log");
  BufferedWriter bw = new BufferedWriter(new FileWriter(file));
  
  for(int i = 1 ; i &lt; 300 ; i++){
      bw.write(i);
  
      if(i % 100 == 0){
          File newFile = new File("filePath2.log");
          if(file.renameTo(newFile)){
               System.out.println("true");
          } else{
               System.out.println("false");
          }
          file = new File("filePath.log");
          bw = new BufferedWriter(new FileWriter(file));
      }
  }
  </code></pre>
  
  <p>Any help is appreciated! </p>
  
  <p>EDIT:</p>
  
  <p>I was able to fix the problem using Files.move method</p>
  
  <pre><code>bw.close();
  String newFilePath  ="C:/opt/streamserve/projroot/applications/RFC_SAP_T1/wd/rfcgateway07122014" + move + ".log";
  File newFile = new File(newFilePath);
  Files.move(file.toPath(), newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
  file = new File("C:/opt/streamserve/projroot/applications/RFC_SAP_T1/wd/rfcgateway.log");
  fw = new FileWriter(file);
  bw = new BufferedWriter(fw);
  move++;
  </code></pre>