• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why doesn't File.renameTo() not always work?

 
Nam Ha Minh
Ranch Hand
Posts: 509
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

I am facing an odd behavior when renaming files. Consider the following code:



- file1 does always exist. I am expecting that file1 is always renamed to file2, but the actual output show this:



That is, sometimes the renameTo() operation does not work. I couldn't understand this odd behavior.

Could anyone have ideas?

Thanks.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't the documentation provide examples of when the renameTo() method will return false?

One that I can think of immediately is that the file described by your "file2" variable's object already exists. I'm sure there are other reasons.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Doesn't the documentation provide examples of when the renameTo() method will return false?

Not really. There are a few examples of reasons for failure, but the list is woefully incomplete. The API and docs for java.io.File have always been pretty crappy. Not GregorianCalendar level of crappy, but pretty crappy.

Nam Ha Minh, if you can use Java 7, I suggest using java.nio.file.Files.move() instead. It is much more likely to provide useful info if it fails.

Otherwise, possible causes of your problem:

The original file may not exist.
You may not have write access to that file or the directory it's in.
The target directory may not exist.
You may not have write access to the target directory.
A file of the same name may already exist.

Now for all these, you may think you know the answer already. But I strongly suggest printing out the values of sourceFile.getCanonicalFile() and targetFile.getCanonicalFile(), to see the actual exact location of the files you're talking about. They may not be where you think they are.

Lastly though, if it's not one of the above reasons, the most common problem is also often the hardest to debug: something, somewhere, is still reading or writing to the file in its current location, and the system won't let you move the file until that thing stops reading or writing. You need to find it and stop it.

So, how is the file being created? Who else edits it? Who reads it?

If the answer is other parts of your own Java program, then the solution will be to call the close() method on all Readers, Writers, or whatever else is using the file. Use a finally blcok to ensure that close is called. Or, if you're on a modern JDK (JDK 6 or later), use try-with-resources. You should always use such techniques anyway, so that you aren't stuck later trying to debug a problem like this.

If the answer is other programs or processes running on the same system, then it's harder, depending on the situation. Find those other processes and kill them, if you can. Otherwise you probably need to reconsider your process and program design, so that your program can work with the other processes, rather than fighting with them.
 
Nam Ha Minh
Ranch Hand
Posts: 509
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Men,

Thank you for your replies. I have got that problem solved. I double checked my code, that sometimes the file1 does not exist so the renameTo() returns false.

Many thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!