How to check a file is directory in java

This example shows you how to check a file is directory or not in java.

To check if an abstract pathname is a directory we can use the File.isDirectory() method.

Below is the example code:

package com.java.connect.io;

//How to check a file is directory in java
import java.io.File;

public class CheckDirectoryExample {
	public static void main(String[] args) {
		// Get the src folder instance
		File file = new File("src");

		// Call isDirectory() method to check
		if (file.isDirectory()) {
			System.out.println("This is a directory.");
		} else {
			System.out.println("This is not a direcroty.");
		}
	}
}

The output of the above example will be as:

This is a directory.