What is FileUtils.isEmptyDirectory() in Java?

Overview

isEmptyDirectory() is a staticMethods in Java that can be called without creating an object of the class. method of the FileUtils class that is used to check whether the directory is empty.

How to import FileUtils

The definition of FileUtils can be found in the Apache Commons IO package, which we can add to the Maven project by adding the following dependency to the pom.xml file:


<dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
</dependency>

For other versions of the commons-io package, refer to the Maven Repository.

You can import the FileUtils class as follows:


import org.apache.commons.io.FileUtils;

Syntax


public static boolean isEmptyDirectory(final File directory)

Parameters

  • final File directory: This is the directory to check.

Return value

This method returns true if the directory is empty. Otherwise, it returns false.

Code

import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
public class Main{
public static void main(String[] args) throws IOException {
// Example 1
String filePath = "/Users/educative/Downloads";
File file = new File(filePath);
System.out.printf("The output of FileUtils.isEmptyDirectory(%s) is - %s", filePath,FileUtils.isEmptyDirectory(file));
System.out.println();
// Example 2
filePath = "/Users/educative/Downloads/temp";
file = new File(filePath);
System.out.printf("The output of FileUtils.isEmptyDirectory(%s) is - %s", filePath,FileUtils.isEmptyDirectory(file));
System.out.println();
// Example 3
filePath = "2.txt";
file = new File(filePath);
System.out.printf("The output of FileUtils.isEmptyDirectory(%s) is - %s", filePath,FileUtils.isEmptyDirectory(file));
}
}

Explanation

Example 1

  • file - "/Users/educative/Downloads"

The method returns false, as the downloads directory is not empty.

Example 2

  • file - "/Users/educative/Downloads"

In this example, we create an empty directory in the Downloads directory using the bash command mkdir.

On passing the empty temp directory, the method returns true, as the directory is empty.

Example 3

  • file - 2.txt

The method throws NoSuchFileException, as the file is not available.

Output

The output of the code will be as follows:


The output of FileUtils.isEmptyDirectory(/Users/educative/Downloads) is - false
The output of FileUtils.isEmptyDirectory(/Users/educative/Downloads/temp) is - false
Exception in thread "main" java.nio.file.NoSuchFileException: 2.txt
	at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)
	at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
	at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116)
	at java.base/sun.nio.fs.UnixFileSystemProvider.newDirectoryStream(UnixFileSystemProvider.java:412)
	at java.base/java.nio.file.Files.newDirectoryStream(Files.java:472)
	at org.apache.commons.io.file.PathUtils.isEmptyDirectory(PathUtils.java:763)
	at org.apache.commons.io.FileUtils.isEmptyDirectory(FileUtils.java:1525)
	at Main.main(Main.java:21)

Free Resources