Forums Register Login

renameTo() problem

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thanks,
the problem was that both files existed within the folder.
Thanks again,
Trish.
Lookout! Runaway whale! Hide behind this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1352 times.
Similar Threads
renameTo()?
Why return false when renaming a file?
Multiple File Save
renameTo() and delete() not working
A doubt about renaming a file
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:04:37.