aspose file tools*
The moose likes Beginning Java and the fly likes Renaming a file -- what's wrong with this code ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Renaming a file -- what Watch "Renaming a file -- what New topic
Author

Renaming a file -- what's wrong with this code ?

Chris Mathews
Ranch Hand

Joined: Jan 08, 2001
Posts: 46
I am puzzled why this rename code is not working.I am able to rename a file within the same directory but when I want to rename a file of a directory to a different name and move to a different directory then it is not working.
Can you tell me why ?
Is the naming convention of directories wrong ?
Your help is well appreciated.
Sarath Mohan
Ranch Hand

Joined: Mar 17, 2001
Posts: 213
Hi Chris,
We cound not rename a file which resides in a different directory, am I right?


Sarath Mohan
Mat Robinson
Greenhorn

Joined: Aug 02, 2002
Posts: 20
I had a very similar problem on Linux, it involved moving a file between mount points. It may have a similar effect in Windows between drives, have you tried moving between directories on the same drive (not declaring the drive)?
Mat.
Chris Mathews
Ranch Hand

Joined: Jan 08, 2001
Posts: 46
Is it so ?
I am not aware of this limitation.
If what you are telling is true then I understand why renaming to a different directory is failing time and time again.
Can somebody provide me more input, please ?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I've seen similar problems on Windows and UNIX systems. I'm not sure exactly what the rules are, as it seems to be system dependent, but it seems that if the two directories are not really on the same machine (i.e. you're depending on some sort of network connection) then the renameTo() may fail. In this case you can often still create a FileInputStream and FileOutputStream to copy bytes from one location to the other, then delete the original. Rather annoying though.
However, I just tested on Windows XP and had no problem renaming from one local drive to another. You could well have a different problem entirely. Does the directory d:/xyz/usa already exist? If not, that's your problem. You need to create the directory first; renameTo() won't do that for you. Also, make sure you have read access to the source file, and write access to the target directory. The renameTo() method is remarkably uninformative about why it may fail. I'd write my own static method which checks all these things and then tries the byte-copying alternative if the renameTo fails.
[ August 08, 2002: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Chris Mathews
Ranch Hand

Joined: Jan 08, 2001
Posts: 46
Thanks Jim for reply.
I am running this code on NT.
Directories are already there and C and D are both local drive and definitely I have the access to these directories.
Copying byte by byte is very slow process esp. when you have large file.But it works !
Thanks anyway.
I am looking for some other alternative.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Try renaming to a new file name in the same directory as the original, to make sure it works. Then try renaming to a different directory on the C drive. Does the problem show up for any rename? Any rename to a different directory? Or just renames to a different drive?
Also, for byte copying be sure you're using a buffer to grab many bytes at once, not just one at a time:

(I believe a BufferedInputStream would be redundant here, but not on the output. May be wrong though...)
Copying is still slower than renaming, but this should be much better than copying one byte at a time.
One other alternative is to use Runtime.exec() to call a system command like rename. It's kind of a pain to set this up right, and you lose platform independence. But it may be worthwhile for some applications.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Renaming a file -- what's wrong with this code ?