Solved

Checking to see existance of a file before creating one.

Posted on 2001-08-14
5
188 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 19

Accepted Solution

by:
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

0
 
LVL 1

Expert Comment

by:Moondancer
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.
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
ID: 9061866
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
ID: 9072415
Recommendation: points to jim_cakalic
0
 
LVL 20

Expert Comment

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

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.

724 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