Java's File.renameTo fails on Windows
11 comments
Comment from: Tony [Visitor]
Thanks for this!!! I was having the exact same problem with the delete always returning false for what seemed like no good reason. Behold - the FileInputStream was still open from reading the properties file!
Comment from: Soner [Visitor]
Thanks a lot for the information. This problem was driving me mad.
Comment from: MrKnödelmann [Visitor]
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6213298
I had the same problem with writing data to a new file and than try to renam it. This worked for me:
try {
fos.close(); // fos is a FileOutputStream
} catch (Exception e) {}
System.gc();
try {
Thread.sleep(50);
} catch (InterruptedException e) { }
hth
Regards
Comment from: Fern [Visitor]
Excellent, because I’m having this problem right now.. I’m glad I found this.. woohoo..
Thanks a bunch..
Comment from: L3x1uTh0r [Visitor]
Perfecto!!! ahora entiendo porque no podia hacer el rename.
Thanks a bunch!!
Comment from: SR [Visitor]
Hi, I am trying to rename the sub folders in about 10 parent folders in the loop. Meanwhile I am uploading the files in to the sub folders and downloading the files, but closing the files. But rename fails some times for 1 or two sub folders randomly if I continuously doing uploading, renaming and downloading. How to solve this problem?
Comment from: davidnewcomb [Member]
Check that you are closing after an exception.
Check that you are not trying to rename a folder while you are touching any of the files contained within it. Under Unix this will be fine but windows is useless and won’t let you alter parents of filesystem entities.
Comment from: Selvadurai [Visitor]
File o=new File("d:/old.txt");
File n=new File("d:/new.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
Comment from: Sathiya [Visitor]
thanks a lot … it really saved the day for me once again thanks:)
Comment from: Terrance [Visitor]
I am working with over 4000 pdfs that I want to sort by size into two separate folders, landscape and portrait folders. I wrote the code to get that done but I am having the same problem with the result that the files are not being renamed to the folders. However, I have to get the pagesize for each pdf file and then move it to the landscape folder or portrait folder depending if height>width. how do I close the inputstream without touching the file and expect the result?
Comment from: davidnewcomb [Member]
I would open the file, find out what you want, close the file then move it to the correct folder.
Form is loading...