Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   How to check if a directory exists? folder.exists() does not work! (http://www.velocityreviews.com/forums/t695980-how-to-check-if-a-directory-exists-folder-exists-does-not-work.html)

Ulf Meinhardt 08-25-2009 12:47 PM

How to check if a directory exists? folder.exists() does not work!
 
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


Andreas Leitgeb 08-25-2009 02:09 PM

Re: How to check if a directory exists? folder.exists() does notwork!
 
Ulf Meinhardt <ulf2m@email.com> 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?


Lew 08-25-2009 03:15 PM

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

Bill McCleary 08-25-2009 03:40 PM

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).

Andreas Leitgeb 08-25-2009 04:03 PM

Re: How to check if a directory exists? folder.exists() does notwork!
 
Lew <lew@lewscanon.com> 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...


Karl Uppiano 08-27-2009 02:21 AM

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


"Andreas Leitgeb" <avl@gamma.logic.tuwien.ac.at> wrote in message
news:slrnh97s49.5sv.avl@gamma.logic.tuwien.ac.at.. .
> Ulf Meinhardt <ulf2m@email.com> 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/ap...getProperties()


Andreas Leitgeb 08-27-2009 05:41 AM

Re: How to check if a directory exists? folder.exists() does notwork!
 
Karl Uppiano <Karl_Uppiano@msn.com> wrote:
> "Andreas Leitgeb" <avl@gamma.logic.tuwien.ac.at> wrote in message
> news:slrnh97s49.5sv.avl@gamma.logic.tuwien.ac.at.. .
>> Ulf Meinhardt <ulf2m@email.com> 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/ap...getProperties()

equivalent (because initialized from the property), but shorter to use:
http://java.sun.com/javase/7/docs/ap...#field_summary


Karl Uppiano 08-28-2009 07:14 AM

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


"Andreas Leitgeb" <avl@gamma.logic.tuwien.ac.at> wrote in message
news:slrnh9c73t.5sv.avl@gamma.logic.tuwien.ac.at.. .

[...]

> 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.

[...]


Lew 08-28-2009 12:26 PM

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


All times are GMT. The time now is 01:32 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57