simple file code

Posted on 2005-04-28
Last Modified: 2010-03-31
I just declared 'aFile'  and now need the code  to verify that 'aFile'  exists and print out "NO such file" if it doesn't

and then I need to  open 'aFile' for input as 'InputFile'

then close 'aFile'
Question by:usumle
    LVL 86

    Expert Comment

    Check the exists() method of
    LVL 5

    Accepted Solution

    //if aFile is ur File Object then,

     System.out.println("No such file");
    LVL 30

    Expert Comment

    >> open 'aFile' for input as 'InputFile'

    What is 'InputFile'? Is it an InputStream (FileInputStream)? Here's how to read bytes from a file into a byte-array:

    And here's how to read from it as text:
    LVL 15

    Expert Comment

    After aFile.exists() [see CEHJ suggestion] (assuming aFile is of type you can also apply aFile.canRead() check to see if your program will be able to read that file (though that will also be detected by actually trying to read it via FileInputStream or a FileReader [see mayankeagle suggestion]).
    LVL 30

    Expert Comment


    Why did you accept that comment?

    - it mentions almost the same thing which CEHJ already mentioned

    - it does not answer your question completely - the other part is answered in my comment

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    Title # Comments Views Activity
    what is spring 3 35
    commonTwo  challenge 63 68
    Receive file in Servlet 1 26
    array6 challenfge 6 48
    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    761 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

    9 Experts available now in Live!

    Get 1:1 Help Now