On this document we will be showing a java example on how to use the isDirectory() method of File Class. This method tests whether the file denoted by this abstract pathname is a directory.

Where it is required to distinguish an I/O exception from the case that the file is not a directory, or where several attributes of the same file are required at the same time, then the Files.readAttributes method may be used.


SecurityException – If a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the file

Method Syntax

public boolean isDirectory()

Method Argument

Data Type Parameter Description

Method Returns

This method returns a boolean, true if and only if the file denoted by this abstract pathname exists and is a directory; false otherwise.


Requires Java 1.0 and up

Java File isDirectory() Example

Below is a java code demonstrates the use of isDirectory() method of File class. The example presented might be simple however it shows the behaviour of the isDirectory() method of File class. We put a check if the file is a directory or not using isDirectory() method. If the result is true, we print all the filename inside the directory.



 * This example source code demonstrates the use of  
 * isDirectory() method of File class.

public class FileIsDirectoryExample {

	public static void main(String[] args) {

		// initialize File object
		File file = new File("C:javatutorialhqinput");
		// check if the file is a directory
		boolean result = file.isDirectory();
		if (result) {
			System.out.println(file.getAbsolutePath() + " is a directory");
			System.out.println("***** Listing all files on the directory *****");
			// listing the files
			String[] list = file.list();
			for (String s : list) {

Sample Output

Below is the sample output when you run the above example.

java lang File isDirectory() example output