Solved

checking if a file exists.

Posted on 2004-10-25
158 Views
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.

/test/test.txt

just append something to it, otherwise i want to create a new one.
0
Question by:pronane
    4 Comments
     
    LVL 35

    Expert Comment

    by:TimYates
    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
     
    LVL 35

    Expert Comment

    by:TimYates
    Oh, and to check if a file exists, do:

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

    Author Comment

    by:pronane
    ya i figured it out already myself

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

    Accepted Solution

    by:
    yup
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Anonabox PRO Tor & VPN Router

    PRO is the most advanced way to fortify your privacy and online anonymity by layering the Tor network with VPN services. Use both together or separately, and without needing to download software onto your devices.

    Suggested Solutions

    Title # Comments Views Activity
    Export JSON response data to Excel in IE9 3 56
    maven scope 1 113
    love6 challenge java 31 120
    build fail in maven project 11 152
    Easy CSR creation in Exchange 2007,2010 and 2013
    Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    913 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

    17 Experts available now in Live!

    Get 1:1 Help Now