• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

renameTo method in io package...

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 9678
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags when you post code.
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic