This post will discuss how to check if a directory exists in Java.

There are several ways to check for the directory’s existence in Java. Each of the following solutions returns true if the directory exists; false otherwise.

1. Using File.isDirectory() method

The idea is to use the File.isDirectory() method to determine whether the file denoted by a specified path is a directory. This method returns true if the directory exists; false otherwise. This is demonstrated below:

Download Code

2. Using NIO

From Java 7 onward, we can use java.nio.file.Files, which provides several static methods that operate on files, directories, or other types of files. To simply check for a directory’s existence, we can use isDirectory() method of java.nio.file.Files class. The Files.isDirectory() returns true when your path points to a directory. This is demonstrated below:

Download Code

That’s all about determining whether a directory exists in Java.