How to check if a directory exists? folder.exists() does not work!

Discussion in 'Java' started by Ulf Meinhardt, Aug 25, 2009.

  1. I would like to check in a Java program if a certain directory exists.
    The following does NOT work:

    String fn = new String("C:\foobarnotexisting");
    File root = new File(fn);

    System.out.println("res=" + root.exists());
    if (!root.exists()) {
    System.out.println("not existing"); }



    After compiling there is NO output.

    Why?

    How else can I check the existence?

    Ulf
     
    Ulf Meinhardt, Aug 25, 2009
    #1
    1. Advertising

  2. Re: How to check if a directory exists? folder.exists() does notwork!

    Ulf Meinhardt <> wrote:
    > I would like to check in a Java program if a certain directory exists.
    > The following does NOT work:
    > String fn = new String("C:\foobarnotexisting");


    Here is a backslash missing. (or, I think, in java you could also use
    unix-like slashes.): thus: "C:\\foo..." or "C:/foo..."

    The \f is taken as some other special character (too lazy to look up, which)
    so, your File object searches for something different.

    Also, there's usually no need for creating a new String(...); you can assign the
    string-literal directly. There are a few cases, where creating a new String(...)
    is useful, but this one doesn't look like one.

    > File root = new File(fn);
    > System.out.println("res=" + root.exists());
    > if (!root.exists()) {
    > System.out.println("not existing"); }


    I'd still have expected this wrong-targetted File to "not exist" and anyway
    I'd have expected the "res=..." line in the output. Perhaps it threw some
    exception that you were catching outside the snippet you posted?
     
    Andreas Leitgeb, Aug 25, 2009
    #2
    1. Advertising

  3. Ulf Meinhardt

    Lew Guest

    Re: How to check if a directory exists? folder.exists() does notwork!

    Ulf Meinhardt wrote:
    >> I would like to check in a Java program if a certain directory exists.
    >> The following does NOT work:
    >> String fn = new String("C:\foobarnotexisting");

    >


    Ulf, you should probably stop cross-posting your queries. Despite
    your "Followup-to:" the conversation got fragmented.

    Andreas Leitgeb wrote:
    > Here is a backslash missing. (or, I think, in java [sic] you could also use
    > unix-like slashes.): thus: "C:\\foo..." or "C:/foo..."
    >


    Java has nothing to do with it - it simply passes along the characters
    to the OS. It is Windows that accepts the forward slash.

    > The \f is taken as some other special character (too lazy to look up, which)


    Form-feed. You never used C, did you?

    > so, your File object searches for something different.
    >


    > I'd still have expected this wrong-targetted  File to "not exist" and anyway
    > I'd have expected the "res=..." line in the output. Perhaps it threw some
    > exception that you were catching outside the snippet you posted?


    This point was addressed in the main branch of this conversation.

    --
    Lew
     
    Lew, Aug 25, 2009
    #3
  4. Re: How to check if a directory exists? folder.exists() does notwork!

    [REPOST: first attempt was supposedly posted successfully, but failed to
    appear within 5 minutes]

    Ulf Meinhardt wrote:
    > I would like to check in a Java program if a certain directory exists.
    > The following does NOT work:
    >
    > System.out.println("res=" + root.exists());


    Try using File.isDirectory() instead (and File.isFile() for
    non-directory files).
     
    Bill McCleary, Aug 25, 2009
    #4
  5. Re: How to check if a directory exists? folder.exists() does notwork!

    Lew <> wrote:
    > Ulf, you should probably stop cross-posting your queries. Despite
    > your "Followup-to:" the conversation got fragmented.


    I chose to ignore the Followup-To: header according to the principle
    "post here - read here". I don't care what other newsgroups one posts
    to, and I do not even care about multipostings (although others do),
    but my answers always go into groups I already read. I don't post into
    c.l.j.h, because I do not read there. If I did, I'd have answered there.

    Maybe I wasted my time that way, and next time I'll just not answer any
    question that has a followup-to somewhere else - thereby punishing (or
    actually gratifying - not sure how valueable my posts are) cross-posters
    in contrast to multi-posters.

    > Andreas Leitgeb wrote:
    >> Here is a backslash missing. (or, I think, in java [sic] you could also use
    >> unix-like slashes.): thus: "C:\\foo..." or "C:/foo..."

    > Java has nothing to do with it - it simply passes along the characters
    > to the OS. It is Windows that accepts the forward slash.


    Acknowledged. Didn't have that OS at hand to check.
    From what I've heard, some dos/windows programs themselves accept
    only backslash-separated paths, but that's a different story.

    >> The \f is taken as some other special character (too lazy to look up, which)

    > Form-feed. You never used C, did you?


    Oh, I did C a lot, and still do C++ a lot, just forgot that tidbid.
    \n, \t, \r, \a, \b and \e... just were so much more common...
     
    Andreas Leitgeb, Aug 25, 2009
    #5
  6. Ulf Meinhardt

    Karl Uppiano Guest

    "Andreas Leitgeb" <> wrote in message
    news:...
    > Ulf Meinhardt <> wrote:
    >> I would like to check in a Java program if a certain directory exists.
    >> The following does NOT work:
    >> String fn = new String("C:\foobarnotexisting");

    >
    > Here is a backslash missing. (or, I think, in java you could also use
    > unix-like slashes.): thus: "C:\\foo..." or "C:/foo..."


    If you are programming in Java, you should really use the platform neutral

    System.getProperty("file.separator");

    instead of embedding platform-specific characters, such as slash or
    backslash.

    http://java.sun.com/javase/7/docs/api/java/lang/System.html#getProperties()
     
    Karl Uppiano, Aug 27, 2009
    #6
  7. Re: How to check if a directory exists? folder.exists() does notwork!

    Karl Uppiano <> wrote:
    > "Andreas Leitgeb" <> wrote in message
    > news:...
    >> Ulf Meinhardt <> wrote:
    >>> I would like to check in a Java program if a certain directory exists.
    >>> The following does NOT work:
    >>> String fn = new String("C:\foobarnotexisting");

    >> Here is a backslash missing. (or, I think, in java you could also use
    >> unix-like slashes.): thus: "C:\\foo..." or "C:/foo..."

    >
    > If you are programming in Java, you should really use the platform neutral
    > System.getProperty("file.separator");
    > instead of embedding platform-specific characters, such as slash or
    > backslash.


    If you're composing some path from externally configured or user-specified
    components, then yes, using "file.separator" property or File.separator
    is the way to go.

    But if a specific path to a specific local file is hardcoded in a
    for-my-own-use application (or a sample), then doing it like:
    "C:"+File.separator+"foobarnotexisting" looks rather goofy to me,
    especially as the volume-part makes it still platform-specific.

    > http://java.sun.com/javase/7/docs/api/java/lang/System.html#getProperties()

    equivalent (because initialized from the property), but shorter to use:
    http://java.sun.com/javase/7/docs/api/java/io/File.html#field_summary
     
    Andreas Leitgeb, Aug 27, 2009
    #7
  8. Ulf Meinhardt

    Karl Uppiano Guest

    "Andreas Leitgeb" <> wrote in message
    news:...

    [...]

    > If you're composing some path from externally configured or user-specified
    > components, then yes, using "file.separator" property or File.separator
    > is the way to go.
    >
    > But if a specific path to a specific local file is hardcoded in a
    > for-my-own-use application (or a sample), then doing it like:
    > "C:"+File.separator+"foobarnotexisting" looks rather goofy to me,
    > especially as the volume-part makes it still platform-specific.


    I just wanted to make the point that using hard-coded separators is
    potentially non-portable, and also causes the kind of problem the OP
    encountered. I hard-code one-off stuff myself, but not for production code.

    [...]
     
    Karl Uppiano, Aug 28, 2009
    #8
  9. Ulf Meinhardt

    Lew Guest

    Re: How to check if a directory exists? folder.exists() does notwork!

    Karl Uppiano wrote:
    > I just wanted to make the point that using hard-coded separators is
    > potentially non-portable, and also causes the kind of problem the OP
    > encountered. I hard-code one-off stuff myself, but not for production code.


    I agree with you, but let me point out that using a hard-coded separator
    didn't cause the OP's problem, solely, but ignorance of escape characters.

    Although I now believe that knowledge of String interning is not an elementary
    skill in Java (but is early-intermediate), surely knowledge of escape
    characters is something to acquire quite early.

    --
    Lew
     
    Lew, Aug 28, 2009
    #9
    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. Pgar
    Replies:
    1
    Views:
    4,928
    =?Utf-8?B?U2FudGhhbmEgR29wYWxhbg==?=
    Nov 15, 2004
  2. Joris De Groote

    Check if a directory exists in a network drive?

    Joris De Groote, Apr 11, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    3,634
    bruce barker \(sqlwork.com\)
    Apr 11, 2006
  3. calca77

    check if a directory exists

    calca77, Jan 9, 2004, in forum: C++
    Replies:
    3
    Views:
    13,568
    Christoph Rabel
    Jan 9, 2004
  4. Jason Stacy
    Replies:
    2
    Views:
    4,769
    Roedy Green
    Apr 27, 2008
  5. Florian Schaf

    Check if directory exists

    Florian Schaf, Dec 18, 2007, in forum: Ruby
    Replies:
    12
    Views:
    660
    Mike Bethany
    Sep 27, 2010
Loading...

Share This Page