• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

File I/O

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

}catch(Exception e){}


How would you know that you didn't get an exception?
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
lalitha kaparapu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Try this code and you will see the exception.
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I tried that code without the catch clause and I got the exception now..Thanks A lot!!!
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
They gave me pumpkin ice cream. It was not pumpkin pie ice cream. Wiping my tongue on this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic