Sunday, 11 June 2017

Java Tutorial : Java IO (Java File - How to check if the directory is empty) ~ foundjava


Click here to watch in Youtube :


FileDemo.java

import java.io.File;
/*
 * Check if a directory is empty
 */
public class FileDemo
{

    private static final String DIR = "D:/work/java";

    public static void main(String[] args)
    {

        File file = new File(DIR);

        if (file.isDirectory())
        {
            if (file.list().length > 0)
            {
                System.out.println("Directory is not empty!");
            }
            else
            {
                System.out.println("Directory is empty!");
            }
        }
        else
        {
            System.out.println("This is not a directory");
        }

    }

}
Output
Directory is not empty!
Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment