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

U

Ulf Meinhardt

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
 
A

Andreas Leitgeb

Ulf Meinhardt said:
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?
 
L

Lew

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

Andreas said:
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.
 
B

Bill McCleary

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

Ulf said:
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).
 
A

Andreas Leitgeb

Lew said:
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 said:
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.
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...
 
A

Andreas Leitgeb

Karl Uppiano said:
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.
equivalent (because initialized from the property), but shorter to use:
http://java.sun.com/javase/7/docs/api/java/io/File.html#field_summary
 
K

Karl Uppiano

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

[...]
 
L

Lew

Karl said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top