Forums Register Login

renameTo is not working

+Pie Number of slices to send: Send
Hi,

I was renaming the folder, which is on the local machine. Here is my code snippet :

File F1 = new File("c:\testing\CV001");
File F2 = new File("c:\testing\CV002");

boolean b = F1.renameTo(F2);

I'm running this program on 2 different PC's, both having the same configuration :
- Windows XP
- Jdk 1.6

On one machine it is renaming successfully. While, on another it is returning me false.
I'm really amazed why such type of thing is happening.
Any suggestions please?

Regards,
Hitesh
+Pie Number of slices to send: Send
Hi Coder,

Welcome to the ranch

Might be the destination file is already present that makes the renameTo method to fail.

+Pie Number of slices to send: Send
 

On one machine it is renaming successfully. While, on another it is returning me false.


That seem to be common for that method. In the Java documentation to renameTo() it says:

Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists.



John
+Pie Number of slices to send: Send
 

John Stark wrote:

On one machine it is renaming successfully. While, on another it is returning me false.


That seem to be common for that method. In the Java documentation to renameTo() it says:

Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists.



John



Thanks for your valuable replies.
In my case, there is only one folder, exist in C: named CV001, which i was trying to rename as CV002 and no such folder with name CV002 is already lying in C:
Since, i'm using the same operating system with same PC configuration.
There could be any other reason for that?
Please suggest, how can i resolve this.

Regards,
Hitesh

1
+Pie Number of slices to send: Send
The path you gave in creating File object is not escaped ("C:\\CV0001" and not "C:\CV0001"). You can try giving lot of printlns and check where exactly the rename method fails.

+Pie Number of slices to send: Send
 

John Jai wrote:The path you gave in creating File object is not escaped ("C:\\CV0001" and not "C:\CV0001"). You can try giving lot of printlns and check where exactly the rename method fails.



Its returning me the "Is successfully rename - false"
I think this is a method problem.
Anyways, Thanks all for your kind support.
1
+Pie Number of slices to send: Send
Hitesh, please PostRealCode. Those two file names you posted won't even compile, because \C is not a valid escape sequence. John was right that you need to escape them.

There are a few reasons why renameTo can fail. The most obvious ones:
- the destination folder doesn't exist.
- the destination file already exists.
- the source file doesn't exist.
- the source file still has a non-closed file input stream or output stream active for it.

The first three you can test with the methods available in java.io.File. The fourth you just need to ensure by closing any file input / output streams in a finally block.
All that thinking. Doesn't it hurt? What do you think about this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 9511 times.
Similar Threads
Need help on File class
NoClassDefFoundError
on File methods
Having a problem in renaming a file
Doubt in Renaming File
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 02:27:41.