In file class we have method called list() it Returns an array of strings naming the files and directories
in the directory denoted by this abstract pathname.So by checking with length we can say directory
is empty or not. Let's go for code..
----------------------------------------------------------------------------------------------------------------------------------
JAVA CODE :
- package com.system;
- import java.io.File;
- public class Test
- {
- public static void main(String[] args)
- {
- File file = new File("D:\\newfolder");
- long n=file.list().length;
- System.out.println("Total files"+n);
- 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");
- }
- }
- }

