java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to check for a directory?

package com.javacoderanch.sample.java.io;

import java.io.File;

public class IsDirectoryExample
{
    public static void main(String[] args)
    {
        // You want to check whether an instance of a file is
        // representating a directory or not.
        File file = new File("FooBar.txt");

        // To do this you can check it by calling isDirectory()
        // method in the File class.
        if (file.isDirectory())
        {
            System.out.println("This file is a directory.");
        } else
        {
            System.out.println("This is just an ordinary file.");
        }
    }
}