• Post Reply Bookmark Topic Watch Topic
  • New Topic

renameTo() problem

 
Trish Hartnett
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I was wondering if anyone could help me please?
I'm using Java1.4 on Windows 2000 and I'm having some difficulty with the renameTo() method.
When I run this code:
import java.io.File;
public class Tester {
boolean isRenamed = true;
public static void main(String[] args){
File newFile1 = new File ("/uploads", "temp.txt");
File newFile2 = new File ("/uploads", "temp2.txt");
boolean isRenamed = newFile1.renameTo(newFile2);
System.out.println(isRenamed);
}
}

I keep on getting false returned. The 2 files do exist, are readable, are not in any TEMP folder and are in the same drive that Java is installed on.
Can anyone suggest anything please ?
Thanks in advance,
Trish.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If both files already exist, then the problem may be that you need to delete the current temp2.txt before you can rename another file to that name.
Other possible reasons for renameTo() to fail: you may not have the appropriate permission (you probably need write permission, not just read, and you may need those permissions for the directory that holds the files, not just the files themselves). And make sure that no other applications are currently holding either file open (like a text editor in another window for example; I always forget and leave something like that open). And in a larger Java program you waouls want to make sure that any InputStream or OutputStream (or Reader or Writer) associated with either file has been closed - use finally to ensure this in most cases.
 
Trish Hartnett
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
the problem was that both files existed within the folder.
Thanks again,
Trish.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!