checking if a file exists.

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.

/test/test.txt

just append something to it, otherwise i want to create a new one.
LVL 1
pronaneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimYatesCommented:
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() ;
0
TimYatesCommented:
Oh, and to check if a file exists, do:

  boolean exists = new File( "/test/test.txt" ).exists() ;
0
pronaneAuthor Commented:
ya i figured it out already myself

i have file f = new file(name)
if f.exists()
0
TimYatesCommented:
yup
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.