Java Check Directory Contains a File or Directory using Apache Commons IO

Tags: FileUtils Apache Commons Apache Commons IO File Directory

In this Java tutorial we learn how to use the FileUtils utility class of Apache Commons IO library to determine whether a parent directory contains a file or a directory.

How to add Apache Commons IO library to your Java project

To use the Apache Commons IO library in the Gradle build project, add the following dependency into the build.gradle file.

implementation 'commons-io:commons-io:2.8.0'

To use the Apache Commons IO library in the Maven build project, add the following dependency into the pom.xml file.

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

To have more information about the Apache Commons IO library you can visit the library home page at commons.apache.org/proper/commons-io/

How to Check Directory Contains a File in Java

Apache Commons IO library provides the method FileUtils.directoryContains() to determine if a parent directory contains a specified file or not as following example code.

CheckDirectoryContainsFile.java

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

public class CheckDirectoryContainsFile {
    public static void main(String... args) {
        try {
            File parentDirectory = new File("D:\\Data");
            File fileToCheck1 = new File("D:\\Data\\data.txt");
            File fileToCheck2 = new File("D:\\TestData\\data1.txt");

            boolean isContains1 = FileUtils.directoryContains(parentDirectory, fileToCheck1);
            boolean isContains2 = FileUtils.directoryContains(parentDirectory, fileToCheck2);

            System.out.println("Directory '" +  parentDirectory + "' contains '" + fileToCheck1 + "' result: " + isContains1);
            System.out.println("Directory '" +  parentDirectory + "' contains '" + fileToCheck2 + "' result: " + isContains2);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
The output is:
Directory 'D:\Data' contains 'D:\Data\data.txt' result: true
Directory 'D:\Data' contains 'D:\TestData\data1.txt' result: false

How to Check Directory Contains a Directory in Java

The following code example shows you how to check if a parent directory contains another directory or not.

CheckDirectoryContainsDirectory.java

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

public class CheckDirectoryContainsDirectory {
    public static void main(String... args) {
        try {
            File directory = new File("D:\\Data");
            File directoryToCheck1 = new File("D:\\Data\\child-dir");
            File directoryToCheck2 = new File("D:\\TestData");

            boolean isContains1 = FileUtils.directoryContains(directory, directoryToCheck1);
            boolean isContains2 = FileUtils.directoryContains(directory, directoryToCheck2);

            System.out.println("Check if directory '" +  directory + "' contains '" + directoryToCheck1 + "' result: " + isContains1);
            System.out.println("Check if directory '" +  directory + "' contains '" + directoryToCheck2 + "' result: " + isContains2);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
The output is:
Check if directory 'D:\Data' contains 'D:\Data\child-dir' result: true
Check if directory 'D:\Data' contains 'D:\TestData' result: false

Happy Coding 😊