wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes renameTo() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "renameTo()" Watch "renameTo()" New topic
Author

renameTo()

Banashri Mandal
Greenhorn

Joined: Jun 12, 2008
Posts: 11
Hello.
I have a doubt in this piece of code.

File oldDir = new File("oldDir") ;
oldDir.mkdir() ;
File f1 = new File ( oldDir, "file1.txt");
f1.createNewFile();
File f2 = new File(oldDir,"file2.txt") ;
f2.createNewFile();
f2.renameTo(new File("newFile.txt"));

Here, after running the code, inside oldDir I get two files.. file1.txt, file2.txt. Can anyone tell me why the last line is not executing ? I mean, oldDir should consists of two files.. file1.txt and newFile.txt ( as f2 is renamed to newFile.txt ) .

Please clarify my doubt.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Very nice search

f2.renameTo(new File("newFile.txt"));


It's creating newFile.txt, but not in the oldDir, but in the directory where your .class file is.

If you want to create file in oldDir, then write
f2.renameTo(new File(oldDir,"newFile.txt"));


SCJP 6
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
I forgot to greet you

Welcome to Javaranch
[ December 23, 2008: Message edited by: punit singh ]
Banashri Mandal
Greenhorn

Joined: Jun 12, 2008
Posts: 11
Thanks.
But please see the code, I have written.
When I run the code, I get file1.txt in oldDir directory and newFile.txt outside of oldDir with "Testing" written in that file.

Please explain why it happens. Does it mean that file2.txt is now outside of oldDir with the new name ?


Please provide me some more info about renameTo() method.
Thanks in advance.

import java.io.* ;

class Test {

public static void main ( String [] args ) {
try {
File oldDir = new File("oldDir") ;
oldDir.mkdir() ;

File f1 = new File ( oldDir, "file1.txt");
f1.createNewFile();
File f2 = new File(oldDir,"file2.txt") ;
f2.createNewFile();

FileWriter fw = new FileWriter(f2);
fw.write("Testing");
fw.flush();
fw.close();

File new1 = new File("newFile.txt") ;
System.out.println(f2.renameTo(new1));
}
catch ( Exception e ) { e.printStackTrace() ; }
}
}
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Banashri.

Now, newFile will land in oldDir.
Banashri Mandal
Greenhorn

Joined: Jun 12, 2008
Posts: 11
Thanks. But I did not want to keep my newFile.txt in oldDir. I am just asking how file2.txt goes to outside of oldDir after the calling renameTo() method.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8871
    
    5
Hey Guys,

For the sake of the authors of mock questions, who work very hard to create them, please ALWAYS tell us where you got any mock question that you discuss here!

Many thanks from all of us authors!

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Banashri Mandal
Greenhorn

Joined: Jun 12, 2008
Posts: 11
Does this group discuss only those questions which we get from some mock test papers ?

I wrote the program by myself. If any mock test contains that question, I don't know about that.

Please explain the output of the code.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8871
    
    5
Hi Banashri,

The code you posted looks like a fragment from a mock exam question. (It also looks a lot like one from K&B, but it's not.) The general idea is to mention where you got a question from - it's totally fine to discuss your own code, and in that case it's probably best to mention that you wrote the code you're discussing.

Thanks,

Bert
[ December 25, 2008: Message edited by: Bert Bates ]
Shwetha Sharma
Ranch Hand

Joined: Jun 01, 2008
Posts: 34

Originally posted by Banashri Mandal:


File oldDir = new File("oldDir") ;
oldDir.mkdir() ;
File f1 = new File ( oldDir, "file1.txt");
f1.createNewFile();
File f2 = new File(oldDir,"file2.txt") ;
f2.createNewFile();
f2.renameTo(new File("newFile.txt"));




When i run this code, my oldDir folder contained only file1.txt ( not the file2.txt) and the newFile.txt was created where my class file was.
When I commented the rename code then both files got created in the oldDir Folder but after running the rename command the file(file2.txt) was renamed and moved outside the folder.

Can someone explain this behavior of rename command.


SCJP6:80%, OCWCD 88 %
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
File oldDir = new File("oldDir") ; --> Here you create a File Object for a directory. Since you did not specify the absolute path for oldDir JVM will take the default path to be relative to your current directory from where you are running your java command. A File object can point to both a file or a directory

oldDir.mkdir(); --> Here you are actually creating the directory in your current working directory.

File f1 = new File ( oldDir, "file1.txt");--> Here you are creating a File object which is actually a file relative to the oldDir.

f1.createNewFile();-->Here you create the file named file1.txt in the folder referred to by oldDir

File f2 = new File(oldDir,"file2.txt") ;--> Here again you are creating a File object which is actually a file relative to the oldDir.

f2.createNewFile();]--> Here you create the file named file2.txt in the folder referred to by oldDir

f2.renameTo(new File("newFile.txt"));--> Here you are moving the file file2.txt from oldDir to your current working directory from where you ran the java command and renaming it to newFile.txt .


thanks
Harvinder
Jyothsna Panchagnula
Ranch Hand

Joined: Jul 11, 2005
Posts: 113


When I excute the above program I see that the file f3 is not renamed to newName.txt also when I tried to print f3.renameto(newName); I get the ouput as false.

Can any one tell me which way should I follow to rename a file?
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
Originally posted by Jyothsana:

When I excute the above program I see that the file f3 is not renamed to newName.txt also when I tried to print f3.renameto(newName); I get the ouput as false.


Well the code works for me in the first run. Please verify the results by deleting any previously created files of the same name.

Also to display the name of the renamed file you need to call
newName.getName();
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: renameTo()