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 sometimes file is not renamed!
I tried to add some Thread.Sleep(BIG_NUMBER) between couple of retries this didn't help.
It looks like the file getting some kind of lock. (when I kill the jvm I can rename the file).
Is there something I can do?
OS: Windows XP, windows 7 JAVA version: 1.5
For the
File.renameTo()
to work,The file will need to be somehow writable by external applications.You can also do something like below:
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.txtHow 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
This is working fine for me. Rename is done using two steps but don't forget to set permissions in
manifest.xml
with: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
Read more here.
From the javadocs:
Note that the Files class defines the move method to move or rename a file in a platform independent manner.
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.