Solved

Checking to see existance of a file before creating one.

Posted on 2001-08-14
5
182 Views
Last Modified: 2010-03-31
Experts,

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
     try
     {
          outputFile = new File("/u/iscdev/Performance/Performance.txt");
          out = new RandomAccessFile(outputFile, "rw");
     }
     catch(java.io.IOException e)
     {
              System.out.println(e.toString());
    }
0
Comment
Question by:gvijay1
5 Comments
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 100 total points
Comment Utility
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

0
 
LVL 1

Expert Comment

by:Moondancer
Comment Utility
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.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

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.
http://www.experts-exchange.com/questions/Q.20092733.html
http://www.experts-exchange.com/questions/Q.20147918.html
http://www.experts-exchange.com/questions/Q.20168330.html
http://www.experts-exchange.com/questions/Q.20205426.html
http://www.experts-exchange.com/questions/Q.20265649.html
http://www.experts-exchange.com/questions/Q.20266870.html
http://www.experts-exchange.com/questions/Q.20269541.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20103690.html

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  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 http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643
 
Thank you everyone.
 
Moondancer
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.
0
 

Expert Comment

by:CleanupPing
Comment Utility
gvijay1:
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
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
Comment Utility
Recommendation: points to jim_cakalic
0
 
LVL 20

Expert Comment

by:Venabili
Comment Utility
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.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venabili
EE Cleanup Volunteer
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

763 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now