Java Program to Check if a Directory is Empty or Not Last Updated : 22 Oct, 2020 Comments Improve Suggest changes Like Article Like Report The list() method defined in class java.io.File which is used to obtain the list of files and folders(directories) present in the specified directory defined by its pathname. The list of files is stored in an array of string. If the length of an array is greater than 0, then the specified directory is not empty, else it is empty. Method 1: Use of list() method Suppose there is a directory present in the path /home/user/folder and it has three text files.Create an array and store the name of files using the list() method.Calculate the length of a string arrayPrint resultSee the below program for the above approach. Java // Java Program to check if // a directory is empty or not import java.io.File; class GFG { public static void main(String[] args) { // mention the directory path File directory = new File("/home/mayur"); if (directory.isDirectory()) { // creating a String Array // store name of files String arr[] = directory.list(); // check if length is greater than 0 or not if (arr.length > 0) { System.out.println("The directory " + directory.getPath() + " is not Empty!"); } else { System.out.println("The directory " + directory.getPath() + " is Empty!"); } } } } Output: Method 2: Use of DirectoryStream Java 7 onwards, the Files.newDirectoryStream method was introduced that returns a DirectoryStream<Path> to iterate over all the entries in the directory. Suppose there is a directory present in the path /home/user/folder and it has three text files.Create a boolean function that checks if the directory is empty or notIf a given path is a file then throw an exceptionIf a given directory has files, return false else true.Print the resultSee the below program for the above approach. Java // Java Program to check if a // directory is empty or not import java.io.File; import java.io.IOException; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; public class GFG { // boolean function which // return true if directory is // empty else return false public static boolean isEmptyDirectory(File directory) throws IOException { // check if given path is a directory if (directory.exists()) { if (!directory.isDirectory()) { // throw exception if given path is a // file throw new IllegalArgumentException( "Expected directory, but was file: " + directory); } else { // create a stream and check for files try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream( directory.toPath())) { // return false if there is a file return !directoryStream.iterator() .hasNext(); } } } // return true if no file is present return true; } public static void main(String[] args) throws IOException { // enter path of directory File directory = new File("/home/mayur"); // call isEmptyDirectory Function if (isEmptyDirectory(directory)) { System.out.println("The directory " + directory.getPath() + " is Empty!"); } else { System.out.println("The directory " + directory.getPath() + " is Not Empty!"); } } } Output: Comment More infoAdvertise with us Next Article Java Program to Check if a Directory is Empty or Not R rohanchopra96 Follow Improve Article Tags : Java Java Programs Practice Tags : Java Similar Reads Java Program to Search for a File in a Directory Searching files in Java can be performed using the File class and FilenameFilter interface. The FilenameFilter interface is used to filter files from the list of files. This interface has a method boolean accept(File dir, String name) that is implemented to find the desired files from the list retur 3 min read Java Program to Check if the TreeMap is Empty The TreeMap in Java is used to implement Map interface and NavigableMap along with the AbstractMap Class. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. Approaches: Using the isEmpty() methodU 3 min read Java Program to Delete a directory The class named java.io.File represents a file or directory (path names) in the system. This class provides methods to perform various operations on files/directories. The delete() method of the File class deletes the files and empty directory represented by the current File object. If a directory i 2 min read Java Program to Get the Size of a Directory The size of files in Java can be obtained using the File class. The in-built function of 'fileName.length()' is used to find size of file in Bytes. The directory may contain 'N' number of files, for calculating the size of the directory summation of the size of all the files is required. length() Me 2 min read Java Directories Programs: Basic to Advanced Directories are an important part of the file system in Java. They allow you to organize your files into logical groups, and they can also be used to control access to files. In this article, we will discuss some of the Java programs that you can use to work with directories. We will cover how to cr 3 min read Like