method to test if a file exist?

Discussion in 'Java' started by jova, Apr 16, 2006.

  1. jova

    jova Guest

    Is there some method I can call that can test to see if a method exist or
    not?
    jova, Apr 16, 2006
    #1
    1. Advertising

  2. jova

    jova Guest

    I'm sorry I meant to say if there is a method that test if a File exist or
    not?
    "jova" <> wrote in message news:dlg0g.362$...
    > Is there some method I can call that can test to see if a method exist or
    > not?
    >
    jova, Apr 16, 2006
    #2
    1. Advertising

  3. jova

    Bjorn Abelli Guest

    "jova" wrote...

    > I'm sorry I meant to say if there is a method that test
    > if a File exist or not?


    Sure, e.g.:

    File f = new File("filename");

    if ( f.exists() )
    {
    // Do something with it...
    }

    // Bjorn A
    Bjorn Abelli, Apr 16, 2006
    #3
  4. Bjorn Abelli wrote:
    > "jova" wrote...
    >
    > > I'm sorry I meant to say if there is a method that test
    > > if a File exist or not?

    >
    > Sure, e.g.:
    >
    > File f = new File("filename");
    >
    > if ( f.exists() )
    > {
    > // Do something with it...
    > }
    >
    > // Bjorn A


    Your method is too complicated. And it does not deal with the possible
    thrown SecurityException. This is the preferred approach

    public static boolean doesExist(java.io.File veryImportantFile)
    {
    try
    {
    veryImportantFile.delete();
    return(false);
    }
    catch(java.lang.SecurityException e)
    {
    return(true);
    }
    }
    Furious George, Apr 16, 2006
    #4
  5. jova

    Roedy Green Guest

    On Sat, 15 Apr 2006 20:38:32 -0400, "jova" <> wrote,
    quoted or indirectly quoted someone who said :

    >I'm sorry I meant to say if there is a method that test if a File exist or
    >not?


    see http://mindprod.com/jgloss/file.html

    File.exists

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Apr 16, 2006
    #5
  6. jova

    Roedy Green Guest

    On 15 Apr 2006 19:23:54 -0700, "Furious George" <>
    wrote, quoted or indirectly quoted someone who said :

    >Your method is too complicated. And it does not deal with the possible
    >thrown SecurityException. This is the preferred approach
    >
    >public static boolean doesExist(java.io.File veryImportantFile)
    >{
    > try
    > {
    > veryImportantFile.delete();
    > return(false);
    > }
    > catch(java.lang.SecurityException e)
    > {
    > return(true);
    > }
    >}


    Newbie alert. He is teasing.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Apr 16, 2006
    #6
  7. jova

    Tony Morris Guest

    On Sat, 15 Apr 2006 19:23:54 -0700, Furious George wrote:

    >
    > Bjorn Abelli wrote:
    >> "jova" wrote...
    >>
    >> > I'm sorry I meant to say if there is a method that test
    >> > if a File exist or not?

    >>
    >> Sure, e.g.:
    >>
    >> File f = new File("filename");
    >>
    >> if ( f.exists() )
    >> {
    >> // Do something with it...
    >> }
    >>
    >> // Bjorn A

    >
    > Your method is too complicated. And it does not deal with the possible
    > thrown SecurityException. This is the preferred approach
    >
    > public static boolean doesExist(java.io.File veryImportantFile)
    > {
    > try
    > {
    > veryImportantFile.delete();
    > return(false);
    > }
    > catch(java.lang.SecurityException e)
    > {
    > return(true);
    > }
    > }


    Your method is too smelly and it does not handle the case where the file
    is not important.

    Prefer this:
    public interface FileExists {
    boolean exists(java.io.File f) throws NullPointerException;
    }

    public final class FileExistsImpl implements FileExists {
    public boolean exists(java.io.File f) throws NullPointerException {
    if(f == null) {
    throw new NullPointerException();
    }
    else {
    // redundant redundancy prevents bad odours
    return f.exists() == true == true == true != false;
    }
    }
    }

    --
    Tony Morris
    http://tmorris.net/

    s/Commonwealth Games/Commonwealth Swimming
    Tony Morris, Apr 16, 2006
    #7
  8. jova

    jova Guest

    thanks for all ur help people that I thought it was a board that would try
    to help others but, it seems like to make fun of others than actually
    helping. I hope your it helped fill the voids that is missing in your life.
    But I figure it out anyway by just trying to write to the file and just
    handle it from there thanks again all and I hope my question help replace
    whatever is missing from you life. Enjoy!!!


    "Tony Morris" <> wrote in message
    news:p...
    > On Sat, 15 Apr 2006 19:23:54 -0700, Furious George wrote:
    >
    >>
    >> Bjorn Abelli wrote:
    >>> "jova" wrote...
    >>>
    >>> > I'm sorry I meant to say if there is a method that test
    >>> > if a File exist or not?
    >>>
    >>> Sure, e.g.:
    >>>
    >>> File f = new File("filename");
    >>>
    >>> if ( f.exists() )
    >>> {
    >>> // Do something with it...
    >>> }
    >>>
    >>> // Bjorn A

    >>
    >> Your method is too complicated. And it does not deal with the possible
    >> thrown SecurityException. This is the preferred approach
    >>
    >> public static boolean doesExist(java.io.File veryImportantFile)
    >> {
    >> try
    >> {
    >> veryImportantFile.delete();
    >> return(false);
    >> }
    >> catch(java.lang.SecurityException e)
    >> {
    >> return(true);
    >> }
    >> }

    >
    > Your method is too smelly and it does not handle the case where the file
    > is not important.
    >
    > Prefer this:
    > public interface FileExists {
    > boolean exists(java.io.File f) throws NullPointerException;
    > }
    >
    > public final class FileExistsImpl implements FileExists {
    > public boolean exists(java.io.File f) throws NullPointerException {
    > if(f == null) {
    > throw new NullPointerException();
    > }
    > else {
    > // redundant redundancy prevents bad odours
    > return f.exists() == true == true == true != false;
    > }
    > }
    > }
    >
    > --
    > Tony Morris
    > http://tmorris.net/
    >
    > s/Commonwealth Games/Commonwealth Swimming
    >
    jova, Apr 16, 2006
    #8
  9. jova

    Alex Hunsley Guest

    jova wrote:
    > thanks for all ur


    It's "your"...

    > help people that I thought it was a board


    Noop, it's a newsgroup. This is Usenet. You may be posting to it via
    google groups, but it's usenet.

    > that would try
    > to help others but, it seems like to make fun of others than actually
    > helping.


    And what was wrong with the answer that Bjorn posted?
    You obviously haven't spent any time reading any other posts if you
    think that no-one gets any help.
    Get over your sense of drive-by entitlement. You got a correct answer,
    and these people aren't your slaves. If you're so disappointed I suggest
    you demand the money back you paid for the advice.
    And it seems you didn't bother to try google, which would have given you
    the answer in a snap, if you'd searched for "java check if file exists"
    or similar.


    > I hope your it helped fill the voids that is missing in your life.


    One of the reasons some people didn't take your question too seriously
    is that you're asking a question which is quite easily answerable by
    using google. It sometimes looks lazy when people ask questions that
    could have been answered well by google. (Isn't necessarily actually
    lazy; sometimes the poster doesn't realise that google is so useful at
    answering questions that way, or isn't sure of the search terms.)

    Hint: anyone who posts questions that include "... and I tried to google
    for phrase 'X' but I didn't turn up anything..." is regarded a little
    more seriously, I find. It doesn't mean that you won't get help if you
    don't saying you used google, though.

    > But I figure it out anyway by just trying to write to the file


    Or you could use Bjorn's answer, which is correct.

    > and just
    > handle it from there thanks again all and I hope my question help replace
    > whatever is missing from you life. Enjoy!!!


    Don't take umbrage because some people had a little fun. Remember,
    you're not paying them for this.
    Alex Hunsley, Apr 16, 2006
    #9
  10. jova

    jova Guest

    "Alex Hunsley" <> wrote in message
    news:vyl0g.67155$...
    > jova wrote:
    >> thanks for all ur

    >
    > It's "your"...
    >
    >> help people that I thought it was a board

    >
    > Noop, it's a newsgroup. This is Usenet. You may be posting to it via
    > google groups, but it's usenet.
    >
    >> that would try to help others but, it seems like to make fun of others
    >> than actually helping.

    >
    > And what was wrong with the answer that Bjorn posted?
    > You obviously haven't spent any time reading any other posts if you think
    > that no-one gets any help.
    > Get over your sense of drive-by entitlement. You got a correct answer, and
    > these people aren't your slaves. If you're so disappointed I suggest you
    > demand the money back you paid for the advice.
    > And it seems you didn't bother to try google, which would have given you
    > the answer in a snap, if you'd searched for "java check if file exists" or
    > similar.
    >
    >
    >> I hope your it helped fill the voids that is missing in your life.

    >
    > One of the reasons some people didn't take your question too seriously is
    > that you're asking a question which is quite easily answerable by using
    > google. It sometimes looks lazy when people ask questions that could have
    > been answered well by google. (Isn't necessarily actually lazy; sometimes
    > the poster doesn't realise that google is so useful at answering questions
    > that way, or isn't sure of the search terms.)
    >
    > Hint: anyone who posts questions that include "... and I tried to google
    > for phrase 'X' but I didn't turn up anything..." is regarded a little more
    > seriously, I find. It doesn't mean that you won't get help if you don't
    > saying you used google, though.
    >
    >> But I figure it out anyway by just trying to write to the file

    >
    > Or you could use Bjorn's answer, which is correct.
    >
    >> and just handle it from there thanks again all and I hope my question
    >> help replace whatever is missing from you life. Enjoy!!!

    >
    > Don't take umbrage because some people had a little fun. Remember, you're
    > not paying them for this.
    >
    >


    I did try it and I read the jdk help I seen no such method as Bjorn stated.
    My question was plain and simple I ask if was there any method such as that
    "I could check if the file existed". I didn't see an adequate reply to my
    question. Then, I get such people trying to to make fun of my question.
    I'm sorry for the incorrect spelling of "urs" but you knew exactly what I
    meant or did you just like I may or may not understand what "I tried to
    google" mean as well. Did you mean search the internet for solution to my
    problem or something else because google is not in the dictionary sorry.
    jova, Apr 16, 2006
    #10
  11. jova

    BlueOpal Guest

    In a multi-threaded environment, file operation or any other resource
    access (which is out of the JVM) needs to be dealt very cautiously. Say
    there are two threads and thread ONE returns TRUE for file.exists() and
    just after that thread TWO deletes this file. Thread ONE would still
    try to access this file resource which now does not exist on the file
    system. So depending on the application we need to judge when to use
    synchronization without paying the price of it in a single threaded
    environment. In case you need to code for the most secure file
    operation, let me know.

    regards
    Amit
    Bangalore
    BlueOpal, Apr 16, 2006
    #11
  12. jova

    raisenero Guest

    raisenero, Apr 16, 2006
    #12
  13. jova

    Alex Hunsley Guest

    jova wrote:
    > "Alex Hunsley" <> wrote in message
    > news:vyl0g.67155$...
    >> jova wrote:
    >>> thanks for all ur

    >> It's "your"...
    >>
    >>> help people that I thought it was a board

    >> Noop, it's a newsgroup. This is Usenet. You may be posting to it via
    >> google groups, but it's usenet.
    >>
    >>> that would try to help others but, it seems like to make fun of others
    >>> than actually helping.

    >> And what was wrong with the answer that Bjorn posted?
    >> You obviously haven't spent any time reading any other posts if you think
    >> that no-one gets any help.
    >> Get over your sense of drive-by entitlement. You got a correct answer, and
    >> these people aren't your slaves. If you're so disappointed I suggest you
    >> demand the money back you paid for the advice.
    >> And it seems you didn't bother to try google, which would have given you
    >> the answer in a snap, if you'd searched for "java check if file exists" or
    >> similar.
    >>
    >>
    >>> I hope your it helped fill the voids that is missing in your life.

    >> One of the reasons some people didn't take your question too seriously is
    >> that you're asking a question which is quite easily answerable by using
    >> google. It sometimes looks lazy when people ask questions that could have
    >> been answered well by google. (Isn't necessarily actually lazy; sometimes
    >> the poster doesn't realise that google is so useful at answering questions
    >> that way, or isn't sure of the search terms.)
    >>
    >> Hint: anyone who posts questions that include "... and I tried to google
    >> for phrase 'X' but I didn't turn up anything..." is regarded a little more
    >> seriously, I find. It doesn't mean that you won't get help if you don't
    >> saying you used google, though.
    >>
    >>> But I figure it out anyway by just trying to write to the file

    >> Or you could use Bjorn's answer, which is correct.
    >>
    >>> and just handle it from there thanks again all and I hope my question
    >>> help replace whatever is missing from you life. Enjoy!!!

    >> Don't take umbrage because some people had a little fun. Remember, you're
    >> not paying them for this.
    >>
    >>

    >
    > I did try it


    What, googling? What did you try googling for? "java check if file
    exists" gets you the answer, anyway...

    > and I read the jdk help I seen no such method as Bjorn stated.


    What exactly do you mean by the jdk help? (Link?)
    If you read the Javadoc API for File you will see a method called exists().
    Did you look for things like File in the java API docs? That's the usual
    starting place (aside from google) if you're trying to find out if a
    capability exists.
    Check out http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html and
    search for "exists()".

    > My question was plain and simple I ask if was there any method such as that
    > "I could check if the file existed". I didn't see an adequate reply to my
    > question.


    Well, Bjorn gave you the correct answer. It was perfectly adequate. If
    you didn't see it as adequate, it's not his fault.

    > Then, I get such people trying to to make fun of my question.


    They weren't taking the mickey out of you particularly, just having some
    fun.

    > I'm sorry for the incorrect spelling of "urs" but you knew exactly what I
    > meant


    Yes, I knew what you meant. However, here are four reasons you should
    use English in this group:

    1) It's the standard language of the group. 'txt speak' isn't. This is
    because:

    2) It looks lazy. You're asking for help here, and yet using
    abbreviations looks like you can't even be bothered to type out your
    question and make yourself understood clearly. Is typing 'your' really
    so much more effort than typing 'ur'?

    3) Not everyone helping here has English as a first language. One guy
    here recently commented that when people write 'ur' etc. he has to stop
    and pronounce it to himself to work out what word the poster really
    meant. If you want help, at least make it as simple as possible for
    people to understand you.

    4) By using text speak you stand out as someone who hasn't lurked in the
    group much or read the java groups FAQ. That lack of effort on your
    behalf can make other people less inclined to help you, even just
    subconsciously!

    > or did you just like I may or may not understand what "I tried to
    > google" mean as well. Did you mean search the internet for solution to my
    > problem or something else because google is not in the dictionary sorry.


    The bottom line on guildeines/rules is the standards of a group, not
    just one simple rule like "You cannot post a word that is not in
    dictionary X". That is why we have newsgroup FAQs. The FAQ for this
    group was posted a couple of days ago.


    (That aside, to 'google', as a verb, actually is in some dictionaries
    now, AFAIK..)
    Alex Hunsley, Apr 16, 2006
    #13
  14. Alex Hunsley wrote on 16.04.2006 10:27:
    > Yes, I knew what you meant. However, here are four reasons you should
    > use English in this group:


    [skipped a lot of good reasons why to use proper english in a posting]

    5) it's the minimum politeness one can ask for!

    I usually don't even bother to answer if a posting is written in poor english.
    Please note that poor english stemming from a non-native speaker is something
    different and can easily be identified as such. But simple laziness is extremely
    impolite IMHO. You wouldn't bother to speak to someone who does not even try to
    utter complete words, wouldn't you?

    Thomas
    Thomas Kellerer, Apr 16, 2006
    #14
  15. jova

    raisenero Guest

    > I usually don't even bother to answer if a posting is written in poor english.

    I agree. When people use NetSpeak to post, I usually skip their
    message entirely. This is especially true in a programming newsgroup
    or forum. It's hard enough interpreting code without piling on the
    additional task of translating their question too. Some of the
    greatest minds involved in technology are non-native English speakers
    (e.g. Linus Torvalds, Bjarne Stroustrup, Satoru Iwata).

    When communicating on the internet, it should always be assumed that
    the receivers of our messages do not speak our language natively. Even
    if the recipient does not speak the language, well-written text can be
    translated by software. Translation services cannot translate
    sms/txt/NetSpeak.

    When asking a question, we want to eliminate as many barriers as
    possible. The question sets the baseline for the quality of the
    answers. It can safely be assumed that every answer will, at best, be
    as clear and concise as the question was.
    raisenero, Apr 16, 2006
    #15
  16. Thomas Kellerer wrote:
    > Alex Hunsley wrote on 16.04.2006 10:27:
    >> Yes, I knew what you meant. However, here are four reasons you should
    >> use English in this group:

    >
    > [skipped a lot of good reasons why to use proper english in a posting]
    >
    > 5) it's the minimum politeness one can ask for!
    >
    > I usually don't even bother to answer if a posting is written in poor
    > english. Please note that poor english stemming from a non-native
    > speaker is something different and can easily be identified as such. But
    > simple laziness is extremely impolite IMHO. You wouldn't bother to speak
    > to someone who does not even try to utter complete words, wouldn't you?
    >
    > Thomas
    >
    >


    and yet you used 'IMHO'

    go figure...
    Andrew McDonagh, Apr 16, 2006
    #16
  17. Andrew McDonagh wrote on 16.04.2006 12:08:
    > and yet you used 'IMHO'
    >
    > go figure...


    You got me there ;)

    But then these are well established abbreviations in newsgroups. I'd assume that
    they are just as "common" as i.e., e.g. or etc.

    But to a certain extent, you are right.

    Thomas
    Thomas Kellerer, Apr 16, 2006
    #17
  18. jova

    Alex Hunsley Guest

    Andrew McDonagh wrote:
    > Thomas Kellerer wrote:
    >> Alex Hunsley wrote on 16.04.2006 10:27:
    >>> Yes, I knew what you meant. However, here are four reasons you should
    >>> use English in this group:

    >>
    >> [skipped a lot of good reasons why to use proper english in a posting]
    >>
    >> 5) it's the minimum politeness one can ask for!
    >>
    >> I usually don't even bother to answer if a posting is written in poor
    >> english. Please note that poor english stemming from a non-native
    >> speaker is something different and can easily be identified as such.
    >> But simple laziness is extremely impolite IMHO. You wouldn't bother to
    >> speak to someone who does not even try to utter complete words,
    >> wouldn't you?
    >>
    >> Thomas
    >>
    >>

    >
    > and yet you used 'IMHO'
    >
    > go figure...


    IMHO and related items are acronyms, as opposed to lazy mispellings of
    words like 'ur'. Not everyone knows what they mean, but they can soon
    find out.
    Alex Hunsley, Apr 16, 2006
    #18
  19. Alex Hunsley wrote:
    > Andrew McDonagh wrote:
    >> Thomas Kellerer wrote:
    >>> Alex Hunsley wrote on 16.04.2006 10:27:
    >>>> Yes, I knew what you meant. However, here are four reasons you
    >>>> should use English in this group:
    >>>
    >>> [skipped a lot of good reasons why to use proper english in a posting]
    >>>
    >>> 5) it's the minimum politeness one can ask for!
    >>>
    >>> I usually don't even bother to answer if a posting is written in poor
    >>> english. Please note that poor english stemming from a non-native
    >>> speaker is something different and can easily be identified as such.
    >>> But simple laziness is extremely impolite IMHO. You wouldn't bother
    >>> to speak to someone who does not even try to utter complete words,
    >>> wouldn't you?
    >>>
    >>> Thomas
    >>>
    >>>

    >>
    >> and yet you used 'IMHO'
    >>
    >> go figure...

    >
    > IMHO and related items are acronyms, as opposed to lazy mispellings of
    > words like 'ur'.


    sure...

    and words like 'ur' are abbreviations.

    Both are perfectly legal English language constructs.

    > Not everyone knows what they mean, but they can soon
    > find out.


    And the same can b said about abbreviations.

    The beauty of Abbreviations is that their true meaning can often be
    discovered by the context or even their placement within the sentence.

    This allows us to create nu 1s on the fly.

    Acronyms however, don't support this.

    Something that makes me smile...

    'GSM SMS messages'

    or expanded to its proper form because acronyms are lazy....

    'Global System for Mobile communication Simple Messaging System messages'

    Both are such a 'mouthful' that they have been shortened to :

    Text Message
    Texts
    txts
    (and others)


    The text part is funny - as its implied in the SMS part of the sentence
    above. But because technology moved on and we got MultiMedia Messaging,
    we introduced the 'text' to differentiate.
    Andrew McDonagh, Apr 16, 2006
    #19
  20. jova wrote:
    >
    > Thanks this seems helpful. Is the exist() method new for the 1.5 jdk
    > because I have an old reference book that I was looking at, the book is
    > based on the1.3 Jdk and I do not see the method in this book or maybe I just
    > overlooked it. Thanks anyway!!!


    It was in JDK 1.00. What is your book about? May I suggest using the API
    documentation (a.k.a. JavaDocs).

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
    Thomas Hawtin, Apr 16, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Y.S.
    Replies:
    3
    Views:
    974
    strajan
    Sep 17, 2003
  2. LT
    Replies:
    7
    Views:
    2,070
    Phlip
    Jul 25, 2004
  3. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    678
    Skybuck Flying
    Oct 4, 2009
  4. Markus Mohr
    Replies:
    7
    Views:
    226
    Thomas 'PointedEars' Lahn
    Nov 28, 2003
  5. Justin
    Replies:
    4
    Views:
    161
    Ben Morrow
    Oct 30, 2003
Loading...

Share This Page