The moose likes Java in General and the fly likes file.renameTo() fails Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "file.renameTo() fails" Watch "file.renameTo() fails" New topic

file.renameTo() fails

ainu neema

Joined: Jul 06, 2004
Posts: 1
C the following code

File theFile = new File("C:/test/batch");

File theFile2 = new File("D:/test/request");

File[] aFile = theFile.listFiles();
for(int x = 0; x < aFile.length ; x++)
System.out.println("Rename the file :"+aFile[x].renameTo(theFile2));

renameTo() function always returns False.
Can anybody helpout..

Petr Blahos
Ranch Hand

Joined: Apr 28, 2004
Posts: 131
it might help to read the documentation:

Whether or not this method can move a file from one filesystem to another is platform-dependent. The return value should always be checked to make sure that the rename operation was successful.

Best regards,

Get a better web browser:<br /><a href="" target="_blank" rel="nofollow"></a>
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
It looks like you are trying to move files from one directory to another, correct? But this code:is actually attempting to rename all files in C:/test/batch to D:/test/batch. Since D:/test/batch is actually a directory that already exists, the renameTo() call fails.

Try modifying your code to something like this:

Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
I agree. Here's the link:
subject: file.renameTo() fails
It's not a secret anymore!