Forums Register Login

Creating file using try and catch

+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send
 

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 ...
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Hi

You may do it as follows-



Hope this clarifies.

+Pie Number of slices to send: Send
 

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()
+Pie Number of slices to send: Send
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();
^

 
+Pie Number of slices to send: Send
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 ==.
+Pie Number of slices to send: Send
 

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".
+Pie Number of slices to send: Send
Thanks for the interest friends.
My problems are solve now
See you very soon with another one
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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();

+Pie Number of slices to send: Send
If you look at the API docs for the File.createNewFile() method, you will find that this code could be simplified.
Your mind is under my control .... your will is now mine .... read this tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 5401 times.
Similar Threads
file renameTo() method
How can I replace the line of file?
File class
Strange File Behavior
File I/O
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 15:25:04.