File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes renameTo method in io package... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "renameTo method in io package..." Watch "renameTo method in io package..." New topic

renameTo method in io package...

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
class DirectoryTest
public static void main(String[] a) throws IOException
File f=new File("dir1");
f.mkdir();//directory created
File f3=new File("directory1");//only object created
f3.mkdir();//another directory created
File f1=new File(f,"gud.txt");
File f2=new File(f,"first.txt");
f1.createNewFile();//file gud.txt is created in dir1
f1.renameTo(f2);/*another file first.txt is created with same data in

f.renameTo(f3);//rename the existing directory....
PrintWriter pw=new PrintWriter(f1);
pw.print("ganesh kumar");
pw.print(" cheekati:");

renameTo method is used to create new file/directory with data which is same in the another directory.

if we take f.renameTo(f3)

dir1 has two files first.txt and gud.txt with data..
now it create new directory with name directory1 which has two files first and gud.txt with data.

In case of using renameTo in file i havenot mention createNewFile() method to create first.txt file.i just use f1.renameTo(f2); it create first.txt file with data.

but incase of directories if there is no f3.mkdir(); it is throwing FileNotFoundException.


if i use it create directory1 dir with same two files like dir1 dir..

can anyone explain me?

[ November 08, 2008: Message edited by: Ganeshkumar cheekati ]

SCJP5 and SCWCD1.5
Think Twice Act Wise...
Ankit Garg

Joined: Aug 03, 2008
Posts: 9465

Well Ganesh you are getting ubnormal behaviour because you are using multiple File objects to represent files inside directories that. Look at this example

If you run this program, it will create a directory named dir1, then rename it to dir2. Neither will it create a new file, nor will it throw any FileNotFoundException. Same is the case with files

Again file1.txt will be created and will be renamed. It will not be copied.

I don't know the exact reason why your program is behaving that way, but it is surely because you are using too many things in it. The most suspicious one I think is using a PrintWriter to write to a File that you have renamed. File objects represent file paths. So the file object after renaming will point to the old path.

This is not complete explanation so let's see what others have to say...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

Please use code tags when you post code.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
hi ankit
i got it now.

In the above code it create a directory with name dirr1 and file1.txt in it.
when line1 is executed it simply changing its name to is not creating any other new directory with name dirr2.

here it create gk1.txt and write text in to that file.
when line2 is executed it simply changes its name to is not creating any new file gk2.txt.

Finally renameTo() method is used to rename the directory/file....

Am i right?
I agree. Here's the link:
subject: renameTo method in io package...
It's not a secret anymore!