tags:

views:

1941

answers:

3

Hi All:

I have a java program which take path as argument. I want to check whether given path is existing or not before doing other validation. Eg: If i give a path D:\Log\Sample which is not not exist, it has to throw filenotfound exception. How can i do that?

+2  A: 

new File( path ).exists().

Read the javadoc its very useful and often gives many useful examples.

mP
+6  A: 
if (!new File("D:\\Log\\Sample").exists())
{
   throw new FileNotFoundException("Yikes!");
}

Besides File.exists(), there are also File.isDirectory() and File.isFile().

Zach Scrivena
+4  A: 

The class java.io.File can take care of that for you:

File f = new File("....");
if (!f.exists()) {
    // The directory does not exist.
    ...
} else if (!f.isDirectory()) {
    // It is not a directory (i.e. it is a file).
    ... 
}
romaintaz