File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

renameTo method in io package...

 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;
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
gud.txt..*/

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


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.

why?

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 ]
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 dirr2.it 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 gk2.txt.it 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: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic