This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes File I/O Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "File I/O" Watch "File I/O" New topic
Author

File I/O

lalitha kaparapu
Greenhorn

Joined: Nov 05, 2007
Posts: 16
import java.io.*;
public class Example4 {
public static void main(String args[]){
try{
File f=new File("directory");
f.mkdir();
File f1=new File(f,"file1.txt");
f1.createNewFile();
File f2=new File(f,"file3.txt");
f2.createNewFile();


PrintWriter p=new PrintWriter(f2);
p.println("lalli");
p.println("swar");
p.flush();
p.close();
System.out.println(f2.delete());
File l=null;
f1.renameTo(l);
}catch(Exception e){}
}
}
I compiled and ran this program.It compiled fan and the file "file3.txt" was renamed to null.But I saw in K&B that we must give the existingFile object a valid new File object with the new name you want(If l had been null we would get a NullPointerException).But I dint get any such exception instead that file was renamed to null.
Can anyone help me in this regard??
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41089
    
  44
}catch(Exception e){}

How would you know that you didn't get an exception?


Ping & DNS - my free Android networking tools app
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Two things:

Firstly, "file3.txt" shouldn't get renamed to null. It should be deleted from your file system.

Secondly, you're not seeing the NullPointerException because your program catches all exceptions and silently handles them. If you remove the try..catch construct, you should see the exception as expected:


SCJP 5.0
lalitha kaparapu
Greenhorn

Joined: Nov 05, 2007
Posts: 16
I am sorry I am taking about "file1.txt"If the catch block is catching that exception Why again the file "file1.txt" is renamed to null.If there is an exception Y should it be renamed??
lalitha kaparapu
Greenhorn

Joined: Nov 05, 2007
Posts: 16
I am sorry I am taking about "file1.txt"If the catch block is catching that exception Why again the file "file1.txt" is renamed to null.If there is an exception Y should it be renamed??
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637

Try this code and you will see the exception.


SCJP, SCWCD, SCBCD
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Originally posted by lalitha kaparapu:
I am sorry I am taking about "file1.txt"If the catch block is catching that exception Why again the file "file1.txt" is renamed to null.If there is an exception Y should it be renamed??


Just tried it myself... Got the Exception.... And the file didn't get renamed.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by lalitha kaparapu:
I am sorry I am taking about "file1.txt"If the catch block is catching that exception Why again the file "file1.txt" is renamed to null.If there is an exception Y should it be renamed??


Did you try the code I gave you earlier? It removes the exception catch clause from your code, so you should see the exception if it occurs.

If you still don't see the exception, perhaps your JRE uses an implementation of File that deviates from Sun's API docs. What JDK / JRE are you using?
lalitha kaparapu
Greenhorn

Joined: Nov 05, 2007
Posts: 16
Yes I tried that code without the catch clause and I got the exception now..Thanks A lot!!!
sapana jain
Ranch Hand

Joined: Oct 24, 2007
Posts: 42
Originally posted by lalitha kaparapu:
Yes I tried that code without the catch clause and I got the exception now..Thanks A lot!!!


i have run your program.i made some changes.i gave that file1.txt should be rename by the name trial.txt but when i am running the code the file1.txt is no more in the directory, so question of replacing it wiht trial.txt.
please help.........
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Try this code


File newName=new File(f,"trial.txt");
Here newName will refer to the file "file1.txt" in the folder "directory". Hence rename will be succesful. Check the return value of the renameTo() method always.
I think you would have tried

Here
File newName=new File("trial.txt");
System.out.println(f1.renameTo(newName));;
Here since newName does not refer to new File object under folder directory. It will create a new file named "trial.txt" under current directory.
Hence to rename a file succesfully under a specific directory the argument to renameTo must refer to a new File object under the same directory as the file that needs to be renamed.
newFile = new File(oldFileDirectory,"NewName.txt");
oldFile.renameTo(newFile);
Hope this clears
Thanks
Deepak
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Having said what i have said
I have few more questions now


Here newName does not point to the folder "directory". When this code is run for the first time the output is true, true, true. Indicatiing a folder "directory" was created and a new file "file1.txt" was created under "directory" and f1.rename(newName) was also successful.
But if the filesystem is observed
a) "directory" is created.
b) file1.txt does not exists under "directory"
c) A new file is created under current folder outside "directory".
1) JAVA API doc for renameTo never mentions that a new file will be created. How come a new file is created "trial.txt"
2) The filesystem does not show the file "file1.txt" under "directory".

3) Now run the same code again the output is false, true, false and the file1.txt is now visible and created under "directory"

Thanks
Deepak
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

a) "directory" is created.
b) file1.txt does not exists under "directory"
c) A new file is created under current folder outside "directory".


Three things are happening (three true in output). The directory has been created -- hence (a) why the directory exists. The file has been created. The file has been renamed to a new name and location -- it is not creating a new file at that location, it is renaming the file from the previous name and location to the new name and location. Hence, why (b) the file is gone. And why (c) it looks like a file has been created.


3) Now run the same code again the output is false, true, false and the file1.txt is now visible and created under "directory"


Only one thing is happening. The file has been created. Hence, why file1.txt exists. The make directory fails. And the rename fails, due to target already in existance -- which is why the file didn't disappear, as it didn't get renamed.

Henry
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
I ran the code with eclipse + debug and observed the following.
First run
1. Folder "directory" is created.
2. File file1.txt is created under directory.
3. Since newName object below is pointing to a new directory location [Current Directory] other than "directory" the call to renameTo actually moved the file file1.txt to current directory and renamed it to "trial.txt"
File newName=new File("trial.txt");
System.out.println(f1.renameTo(newName));;

Second run:
false, true, false
a) faslse: because directory already exists.
b) true, a New file file1.txt is created under "directory" because it does not exists.
c) renameTo failed because there is already a file "trial.txt" exists under current directory.

Further:
I read renameTo API javadoc. It cleary says that renameTo() does not move a file but the first run did move the file. Is it contradicting?

Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

Thanks
Deepak
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File I/O
 
Similar Threads
renameTo()
NoClassDefFoundError
files are not beened deleted and renamed
NoClassDefFoundError
Files from a directory is not being recognized