Checking to see existance of a file before creating one.

Posted on 2001-08-14
Last Modified: 2010-03-31

I am creating a servlet where in the Init() method, I am creating a file. however, I need to modify it and am not sure how to do it.

when the servlet is loaded, and the init method called, I need to first check to see if there is an existing file before before creating the file. This is to ensure that I do not overwrite the data from a day before. This is the code that I have currently to create a new file. Could you please modify it to include the logic to check to see if this particular file exists in the given directory before creating it. Thanks,

     //Now to set up the Performance log File initially when the servlet is loaded
          outputFile = new File("/u/iscdev/Performance/Performance.txt");
          out = new RandomAccessFile(outputFile, "rw");
     catch( e)
Question by:gvijay1
LVL 19

Accepted Solution

Jim Cakalic earned 100 total points
ID: 6384698
Use the File.exists method. You can do this even before attempting to open. Something like this, for example:

outputFile = new File("/u/iscdev/Performance/Performance.txt");
if (outputFile.exists() == false) {
    // file doesn't exist
    out = new RandomAccessFile(outputFile, "rw");
} else {
    // do whatever

Best regards,
Jim Cakalic


Expert Comment

ID: 6853281
Question(s) below appears to have been abandoned. Your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you. You must tell the participants why you wish to do this, and allow for Expert response.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question. Again, please comment to advise the other participants why you wish to do this.

For special handling needs, please post a zero point question in the link below and include the question QID/link(s) that it regards.
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process.

Please click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.

To view your open questions, please click the following link(s) and keep them all current with updates.

To view your locked questions, please click the following link(s) and evaluate the proposed answer.

------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link
Thank you everyone.
Moderator @ Experts Exchange

P.S.  For any year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.

Expert Comment

ID: 9061866
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.
LVL 19

Expert Comment

by:Jim Cakalic
ID: 9072415
Recommendation: points to jim_cakalic
LVL 20

Expert Comment

ID: 9330386
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points for jim_cakalic

Please leave any comments here within the next seven days.
EE Cleanup Volunteer

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 79
Windows 10 IE Certificate Issue 10 45
tomcat administrtor 12 46
hibernate example for saving data 19 37
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question