Sample Programs

Sample java program to check if the directory is empty or not

 package org.jusfortechies.example; 

 import java.io.File;

 public class CheckDirectoryStatus {

   public static void main(String[] args) {
      
      //Give directory path
      File file = new File("/home/username/test");

      // Check if the file represent a directory.
      if (file.isDirectory()) {

         // Get list of files in the directory. 
         String[] files = file.list();

         // If the list size is zero, then its empty
         if (files.length <= 0) {
            System.out.println("The directory - " + file.getPath() + " is empty.");
         }else{
            System.out.println("The directory - " + file.getPath() + " is not empty.");
         }
      }
      
   }

 }




blog comments powered by Disqus