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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
TimYatesConnect With a Mentor Commented:
yup
0
 
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
All Courses

From novice to tech pro — start learning today.