Re: How to check if a directory exists? folder.exists() does not
work!
On Tue, 25 Aug 2009, 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");
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?
Because you're doing something wrong.
Code:
import java.io.*;
public class Ulf {
public static void main(String... args) {
String fn = new String("foobarnotexisting");
File root = new File(fn);
System.out.println("res=" + root.exists());
if (!root.exists()) {
System.out.println("not existing");
}
}
}
Output:
res=false
not existing
I changed the path because i'm on unix, not windows, but i doubt that
would make a difference.
Compile and run exactly the above code, without any changes, on your
machine, and tell us the output.
Then tell us what your original code actually was.
tom
--
They Set Up Us The Revolution - Now We Have Set Up It Them Back