Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking if a file exists.... 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0

Hey everyone,

I have a question regarding error checking with files. In my program, I create the file backUp.html (which I write to)..by doing so:

PrintWriter pw = new PrintWriter(new FileWriter("C:/Windows/Desktop/backUp.html"));

However, what if the file backUp.html already exists? I think it would be nice if I could prompt the user and ask him/her if he/she would like to overwrite the file before it automatically does so...!

Is there a way to do this?

Any feedback will be appreciated.

Thanks in advance,

J.Kamdar



 
Of course you can.

...
File f = new File("c:\windows\desktop\backup.html");
if (f.exists()) // check if file exists
// prompt user
...


Regards,
Leon
 

Thanks...

I can still use that method even with PrintWriters? I will give it a try.

Thanks for the help.
 
Yes of course. Do the check before using your PrintWriter.

Leon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top