Jun 7, 2018

How To Check If Directory Is Empty In Java

You can check length of list() as following:
File file = new File("/i88.ca");
if(file.isDirectory()){
                if(file.list().length>0){
   //Directory is not empty
  }else{
   //Directory is empty
  }
}

With JDK >= 7 you can use Files.newDirectoryStream to open the directory and then use the iterator's hasNext() method to test there are any files to iterator over. This should work better for huge directories or where the directory is on a remote file system when compared to the java.io.File list methods.