Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

renameTo()

 
Banashri Mandal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"));
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to greet you

Welcome to Javaranch
[ December 23, 2008: Message edited by: punit singh ]
 
Banashri Mandal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Banashri.

Now, newFile will land in oldDir.
 
Banashri Mandal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Banashri Mandal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Jyothsna Panchagnula
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic