Saturday, 8 June 2013

Code to verify the given path is file or directory

Code
---------------
import java.io.File;

class CheckFile {

public static void main(String args[]) {
File file = new File("C:/java");

// Way-1
if (file.isDirectory())
System.out.println(file.getName() + " is a directory");
else
System.out.println(file.getName() + " is a file");

// Way-2
if (file.isFile())
System.out.println(file.getName() + " is a file");
else
System.out.println(file.getName() + " is a directory");
}

}

Sample Output
------------------
java is a directory
java is a directory

No comments:

Post a Comment