Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

renameTo is not working  RSS feed

 
Hitesh Kumar C
Ranch Hand
Posts: 35
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Coder,

Welcome to the ranch

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

 
John Stark
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Hitesh Kumar C
Ranch Hand
Posts: 35
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Hitesh Kumar C
Ranch Hand
Posts: 35
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!