aspose file tools*
The moose likes I/O and Streams and the fly likes File.renameTo method question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "File.renameTo method question" Watch "File.renameTo method question" New topic
Author

File.renameTo method question

Jiang Lin
Greenhorn

Joined: Mar 03, 2004
Posts: 6

When running the multiple instances of the above class concurrently
1) from different JVM
2) against the same directory
3) with different instance id (so that multiple instances will not operate on the same file)
the line 28 file.renameTo() method returns false, even though when the file name is successfully renamed.
However, when Thread.sleep() method call in line 21 is commented out, the problem does not occur anymore.
Any idea why the return value of file.renameTo method call indicates a failure when in fact it really worked? Could this be a coding issue, JVM issue (Java 1.3.1), or OS issue (NT 4.0)?
Any insights are greatly appreciated.
Jiang
[ April 02, 2004: Message edited by: Jiang Lin ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Any idea why the return value of file.renameTo method call indicates a failure when in fact it really worked?
Are you sure it really worked? One reason renameTo() may fail is if there's already a file matching the new name. If you run your program more than once, maybe the file got moved correctly the first time. On subsequent attempts the rename fails, because the previously moved file is still there. But it looks as though you've moved it successfully, because there's a file there where you expect the renamed file to be. Try checking if (newFile.exists()) in your method.


"I'm not back." - Bill Harding, Twister
Jiang Lin
Greenhorn

Joined: Mar 03, 2004
Posts: 6
Thanks very much, Jim. That was exactly the problem. There was a bug in instantiating the file filer at line 10, so the instances "saw" the files that were supposed to belong to others and processed them before the supposed owner picked them up...
Thanks again. I very much appreciate your help.
Jiang Lin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File.renameTo method question