• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

renameTo() problem

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,
the problem was that both files existed within the folder.
Thanks again,
Trish.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic