In Java, how to check if a file is directory or not?

We can use isDirectory() method of File object to check if a given file is a directory or not.
String filePathString = "C:\\test.txt";

File f = new File(filePathString);
if ( f.exists() && !f.isDirectory() ) {
    System.out.println("File is not a directory");
} else {
    System.out.println("Either file is missing or is a directory");
}

0 comments:

Post a Comment