How do I determine if a pathname is a directory?

To determine if an abstract pathname is a directory we can use the File.isDirectory() method. Here is an example code.



public class IsDirectoryExample {
    public static void main(String[] args) {
        // Creates a instance of File.
        File file = new File("/home/foo/bar");

        // Check if the abstract pathname is a directory by calling
        // isDirectory() method of the File class.
        if (file.isDirectory()) {
            System.out.println("This file is a directory.");
        } else {
            System.out.println("This is just an ordinary file.");


Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. Support me, buy me ☕ or 🍵

Leave a Reply