wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Creating file using try and catch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating file using try and catch" Watch "Creating file using try and catch" New topic
Author

Creating file using try and catch

Nelson Sam
Ranch Hand

Joined: Jun 12, 2010
Posts: 30
I want to create a new file using try checking if the file already exists and then if it exists if should be handled by catch.
I am able to create a new file when I simply give new File(f).createNewFile(); but when I give a directory
new File(f,"C:\\temp").createNewFile(); It throws system cannot find path specified.
Also I dont know how to check if a file already exists

Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 537

new File(f,"C:\\temp").createNewFile();


In the above part of code, the first argumet should be path and the second argument should be the file name. you have interchanged that. change the above to ...
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3671
    
  15
You've got your parameters the wrong way round.
is trying to create a file called c:\temp in a directory called ifile.txt.

As for checking if a file exists, the File class has a very suitably named method. Check the Javadoc.


Joanne
kunu patil
Greenhorn

Joined: Sep 24, 2007
Posts: 20
Hi

You may do it as follows-



Hope this clarifies.

Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
Nelson Sam wrote:Also I dont know how to check if a file already exists

The API docs are your friend. Why not use exists() method of File class?
-> http://java.sun.com/javase/6/docs/api/java/io/File.html#exists()


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
Nelson Sam
Ranch Hand

Joined: Jun 12, 2010
Posts: 30
Appreciate everyone for helping

@kunu patil-Can you explain more about fileInstance.I could not find in javadocs.Thanks for adding the required codes

@Peter Taucher-Thanks for link.I tired using exists method.



Isn't above code correct.When I compile i get this error

filez.java:11: cannot find symbol
symbol : method exists()
location: class java.lang.String
boolean b=f.exists();
^
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3671
    
  15
f is a String.
exists() is a method of the File class.

And don't use if (b == true), just use if (b). It avoids possible problems with mixing up = and ==.
kunu patil
Greenhorn

Joined: Sep 24, 2007
Posts: 20
Nelson Sam wrote:Appreciate everyone for helping

@kunu patil-Can you explain more about fileInstance.I could not find in javadocs.Thanks for adding the required codes

@Peter Taucher-Thanks for link.I tired using exists method.



Isn't above code correct.When I compile i get this error

filez.java:11: cannot find symbol
symbol : method exists()
location: class java.lang.String
boolean b=f.exists();
^


Hi,

fileInstance is an reference variable that holds reference to a File object. If you want to see JavaDoc for this you should search for "File" and not for "fileInstance".
Nelson Sam
Ranch Hand

Joined: Jun 12, 2010
Posts: 30
Thanks for the interest friends.
My problems are solve now
See you very soon with another one
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
It would only be polite to let the other people here around know 'how' you solved your problem. It even might help further help seeking persons sometime.
Nelson Sam
Ranch Hand

Joined: Jun 12, 2010
Posts: 30
Thanks for suggestion Peter Taucher.

Here is how I solved the problem.

OLD ONE


NEW ONE


Earlier I had assigned the file name to string f.and checked using exists() method of file class,Since f is a string and doesnt belong to a file class f.exists() didnt work.

So I corrected it to
File f;
f=new File("nel.txt");
Boolean check=f.exists();

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3671
    
  15
If you look at the API docs for the File.createNewFile() method, you will find that this code could be simplified.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Creating file using try and catch