This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
    Bookmark Topic Watch Topic
  • New Topic

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

 
Chris Mathews
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 213
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi Chris,
We cound not rename a file which resides in a different directory, am I right?
 
Mat Robinson
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Report post to moderator
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 ]
 
Chris Mathews
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
    Bookmark Topic Watch Topic
  • New Topic