I have eclipse plugin jface application. A thread writes file via BufferedWriter. After writing is done I close the buffer after that I try to rename the file.
But
For the File.renameTo() to work,The file will need to be somehow writable by external applications.
We have had issues under Windows 7 with UAC and unexpected file permissions.  File#canWrite will return true even though any attempts to perform file I/O will fail.
This is working fine for me. Rename is done using two steps but don't forget to set permissions in manifest.xml with:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
public boolean RenameFile(String from, String to) { 
  to.replace(" ", ""); // clear all spaces within file name
  File oldfile = new File(from);
  File newfile = new File(to);
  File tempfile = new File(to + ".tmp"); // add extension .tmp
  oldfile.renameTo(tempfile);
  return (tempfile.renameTo(newfile));
}
                                                                        File.RenameTo() is platform dependent and relies on a few conditions to be met in order to succesfully rename a file, a better alternative is using
Path source = currentFile.toPath();
try {
     Files.move(source, source.resolveSibling(formattedName));
} catch (IOException e) {
     e.printStackTrace();
}
Read more here.
From the javadocs:
Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.
Note that the Files class defines the move method to move or rename a file in a platform independent manner.
You can also do something like below:
File o=new File("oldFile.txt");
File n=new File("newFile.txt");
n.delete();
o.renameTo(n);
n.delete() : We need to delete the file(new.txt) if exists.
o.rename(n) : so that the file(old.txt) is renamed as new.txt
How to find out why renameTo() failed?
Reliable File.renameTo() alternative on Windows?
http://www.bigsoft.co.uk/blog/index.php/2010/02/02/file-renameto-always-fails-on-windows