Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


checking if a file exists.

Posted on 2004-10-25
Medium Priority
Last Modified: 2010-04-01
I want to be able to do a check to see if a file exists and if it does i want to append something to
the end of the file, otherwise I want to create a new file.

so for example, if the file exists in the given directory and there is something written in it.  I want to be able to check the first word and do a comparison on it with a string in my code.


just append something to it, otherwise i want to create a new one.
Question by:pronane
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
  • 3
LVL 35

Expert Comment

ID: 12399596
FileWriter( "/test/test.txt", true ) ;

will create the file if it doesn't exist (or append to it if it does)

To read the first line out of the file, you will need to use

BufferedReader br = new BufferedReader( new FileReader( "/test/test.txt" ) ) ;
String line = br.readLine() ;
LVL 35

Expert Comment

ID: 12399962
Oh, and to check if a file exists, do:

  boolean exists = new File( "/test/test.txt" ).exists() ;

Author Comment

ID: 12400168
ya i figured it out already myself

i have file f = new file(name)
if f.exists()
LVL 35

Accepted Solution

TimYates earned 150 total points
ID: 12400183

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Sometimes Administrators rights are not enough. These cases call for the SYSTEM account. The process in this article outlines the steps required to execute commands using the SYSTEM account.
Tech spooks aren't just for those who are tech savvy, it also happens to those of us running a business. Check out the top tech spooks for business owners.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

598 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