• 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() method in the File Class

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!!! Why is it that the following code prints the original filename considering that the original file was renamed to "testRename.txt"? Need your help.

import java.io.*;
class TestFileClass {
public static void main(String[] args) throws IOException {

new File("test.txt"); // does not create a new file

// create a file in the current directory
File f = new File("test1.txt");
System.out.println( f.createNewFile() );

// rename a file
File f2 = new File("testRename.txt");
System.out.println(f.renameTo(f2)); // renames the file on the system
System.out.println( f.getAbsolutePath() ); // returns original filename
System.out.println( "Does old file exist?: " + f.exists() );
System.out.println( "Does new file exist?: " + f2.exists() );


}


}
OUTPUT:
true
true
c:\test1.txt
Does old file exist?: false
Does new file exist?: true

Thank you,
TINA
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tina,
1. new File("a.txt") doesn't create a new file.
2. To create a file you must create a new File object and call createNewFile() method();
File f = new File("test1.txt");
f.createNewFile();
3. f.renameTo(f1) renames f1 to f2. ( But it doesn't change path field). You can check this method() in API. There is no update of path variable.

That's all.
Jamal
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the renameTo method (along with some others) was pretty well covered in this thread.
Corey
 
Tina Ang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jamal,
Actually I did #2 in the code above:
// create a file in the current directory
File f = new File("test1.txt");
System.out.println( f.createNewFile() );
I'm confused with # 3
f.renameTo(f1) renames f1 to f2. ( But it doesn't change path field).
Thanks for all the help!!!
TINA
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look through the thread I cited above. The File object in immutable in Java. Therefore, if you create a File object that points to "C:\test.txt", and then rename that file to "C:\test_new.txt", the File object you created will still refer to the file "C:\test.txt", which no longer exists.
It's all in that thread - look through it.
Corey
 
Tina Ang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it!!! Thanks Corey!
TINA
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic