Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating file using try and catch

 
Nelson Sam
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 667
4
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
kunu patil
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

You may do it as follows-



Hope this clarifies.

 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()
 
Nelson Sam
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the interest friends.
My problems are solve now
See you very soon with another one
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the API docs for the File.createNewFile() method, you will find that this code could be simplified.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic