How do I check if a directory is not empty?

package org.kodejava.example.io;

import java.io.File;

public class EmptyDirCheck {
    public static void main(String[] args) {
        File file = new File("C:/Users/kodejava/Documents");

        // Check to see if the object represent a directory.
        if (file.isDirectory()) {
            // Get list of file in the directory. When its length is not zero
            // the folder is not empty.
            String[] files = file.list();

            if (files != null && files.length > 0) {
                System.out.println("The " + file.getPath() + " is not empty!");
            }
        }
    }
}

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.

package org.kodejava.example.io;

import java.io.File;

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.");
        }
    }
}