Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

file.renameTo() fails

 
ainu neema
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic