In this example, let’s us see how to determine whether a folder is empty in Java.
Source Code
package com.beginner.examples;
import java.io.File;
import java.io.IOException;
public class CheckDirectorExample {
/**
* @param args
*/
public static void main(String[] args)throws IOException
{
//create dir Test
File dir = new File("Test");
//Show me the status
System.out.println("Whether it was created successfully: "+dir.mkdir());
//First check if it is a folder
if(dir.isDirectory())
{
String[] strDir=dir.list();
if(strDir.length == 0)
{
System.out.println("This folder is empty");
}
else {
System.out.println("This folder is not empty");
}
}
else
{
System.out.println("It's not a folder");
}
}
}
Output:
Whether it was created successfully: true
This folder is empty
References
Imported packages in Java documentation: