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
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 ...
@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".
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.
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();