java

How to Check if a Folder is Empty in Java

In this tutorial, we are going to see how to check if a folder is empty or not in Java by checking its length with the file.list().length method.
 

Java Program to Check if a Folder is Empty:
import java.io.File;

public class Main
{
    public static void main(String[] args)
    {
        File file = new File("C:\\folder");
        if(file.isDirectory()){
            if(file.list().length > 0){
                System.out.println("The directory is not empty!");
            }else{
                System.out.println("The directory is empty!");	
        }	
        }else{	
            System.out.println("This is not a directory!");
        }
    }
}

Output:

The directory is not empty!
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *