File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "file.renameTo() fails" Watch "file.renameTo() fails" New topic
Author

file.renameTo() fails

ainu neema
Greenhorn

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

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

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

try
{
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..

Thanx
Petr Blahos
Ranch Hand

Joined: Apr 28, 2004
Posts: 131
Hi,
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,
Petr


Get a better web browser:<br /><a href="http://www.mozilla.org/products/firefox/switch.html" target="_blank" rel="nofollow">http://www.mozilla.org/products/firefox/switch.html</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"
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: file.renameTo() fails
 
Similar Threads
struts file upload problem
Read from file and display nonduplicate words in ascending order.
Need help on File class
Renaming a file in Java
File renameTo not working