Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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

 
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.
 
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?
 
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 ?
 
Wanderer
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
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.
 
I am displeased. You are no longer allowed to read this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!