Java 8+ version:

import java.nio.file.Paths;
import java.nio.file.Files;

Files.createDirectories(Paths.get("/Your/Path/Here"));

The Files.createDirectories() creates a new directory and parent directories that do not exist. This method does not throw an exception if the directory already exists.

Answer from Inês Gomes on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java io › create a directory in java
Create a Directory in Java | Baeldung
January 8, 2026 - As the new_directory doesn’t exist mkdir doesn’t create the underlying nested_directory. However, the File class provides us with another method to achieve that – mkdirs(). This method will behave like mkdir() but will also create all the unexisting parent directories as well. In our previous example, this would mean creating not only nested_directory, but also new_directory.
🌐
Medium
medium.com › @AlexanderObregon › javas-files-createdirectories-method-explained-cb824678b0b7
Java’s Files.createDirectories() Method Explained | Medium
January 26, 2025 - If the directory doesn’t exist, it creates it along with any nonexistent parent directories. If the path points to a file (not a directory), it throws an exception.
🌐
Mkyong
mkyong.com › home › java › how to create directory in java
How to create directory in Java - Mkyong.com
July 29, 2020 - In Java, we can use the NIO `Files.createDirectories` to create a directory including all nonexistent parent directories.
🌐
Coderanch
coderanch.com › t › 708245 › java › Creating-directory-doesn-exist-writing
Creating a directory if it doesn't exist and then writing a file (I/O and Streams forum at Coderanch)
April 2, 2019 - If you create a File object for the directory you want to create you can use the isDirectory() and exists() methods to determine if a directory (or file) with that name already exists and if it doesn't use the mkDir() or mkDirs() method to create the directory.
🌐
ZetCode
zetcode.com › java › createdirectory
Java create directory - learn how to create a directory in Java
A Path is created from the file name. A Path is a Java object used to locate a file in a file system. if (!Files.exists(path)) { We first check if the directory does not already exist with Files.exists. Files.createDirectory(path); The directory is created with Files.createDirectory.
🌐
IQCode
iqcode.com › code › java › java-create-directory-if-not-exists
java create directory if not exists Code Example
September 8, 2021 - String path = ... File pathAsFile = new File(path); if (!Files.exists(Paths.get(path))) { pathAsFile.mkdir(); } ... Java 2022-03-27 21:50:15 Compilation is not supported for following modules: android.
🌐
W3Docs
w3docs.com › java
Create a directory if it does not exist and then create the files in that directory as well
To create a directory in Java if it does not exist, you can use the java.nio.file.Files class and its createDirectory method. Here is an example of how you can do this: · This code creates a Path object representing the directory and then checks if it exists using the exists method.
Find elsewhere
🌐
Java67
java67.com › 2014 › 02 › how-to-create-file-and-directory-in-java.html
How to Create File and Directory in Java with Example | Java67
That's all about how to create or make files and directories in Java. ... There are a few errors in your code. 1. The code File directory = new File(dir); if (directory.exists()) { ...
🌐
javathinking
javathinking.com › blog › create-a-directory-if-it-does-not-exist-and-then-create-the-files-in-that-directory-as-well
How to Create a Directory If It Doesn’t Exist and Create Files in It (Even for Existing Directories) – Java Code Tutorial
This tutorial will guide you through the process step-by-step, using modern Java APIs (NIO.2) for robust and efficient file operations. We’ll cover checking directory existence, creating directories (including nested ones), creating files, handling exceptions, and advanced scenarios.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-create-a-directory-in-java
How to Create a Directory in Java? - GeeksforGeeks
July 23, 2025 - // Java Program to Create Current Directory import java.io.File; // Driver Class public class GFG { // main function public static void main(String[] args) { // Sprecify the Directory Name String directoryName = "My_Learning"; // Address of Current Directory String currentDirectory = System.getProperty("user.dir"); // Specify the path of the directory to be created String directoryPath = currentDirectory + File.separator + directoryName; // Create a File object representing the directory File directory = new File(directoryPath); // Attempt to create the directory boolean directoryCreated = directory.mkdir(); if (directoryCreated) { System.out.println("Directory created successfully at: " + directoryPath); } else { System.out.println("Failed to create directory. It may already exist at: " + directoryPath); } } }
🌐
How to do in Java
howtodoinjava.com › home › i/o › creating new directories in java
Creating New Directories in Java
April 21, 2022 - Notice the data is the parent directory of archive. In runtime, it is possible that data directory may not exist when we try to create archive directory. We will learn to create the archive directory in such a way: ... The createDirectory() creates the new directory if all the parent directories exist.
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › io › dirs.html
Creating and Reading Directories (The Java™ Tutorials > Essential Java Classes > Basic I/O)
You can create a temporary directory using one of createTempDirectory methods: createTempDirectory(Path, String, FileAttribute<?>...) ... The first method allows the code to specify a location for the temporary directory and the second method creates a new directory in the default temporary-file ...
🌐
javaspring
javaspring.net › blog › java-create-directory-if-not-exists
Java: Create Directory If Not Exists — javaspring.net
In the examples above, we catch the IOException when using Java NIO and check the return value of the mkdirs() method when using the File class. This helps to ensure that your application can gracefully handle situations where directory creation fails. Before attempting to create a directory, it is a good practice to check if it already exists.
🌐
Java Development Journal
javadevjournal.com › home › how to create directory in java
How to create directory in Java | Java Development Journal
June 14, 2017 - It will throw FileAlreadyExist... operation · For creating all nonexistent parent directories first, createDirectories method should be used · public class CreateDirectories { public static void main(String[] args) ...
🌐
Attacomsian
attacomsian.com › blog › java-create-directory
How to create a directory in Java
December 13, 2019 - It doesn’t throw an exception if the directory already exists. Here is an example: try { // directory path Path path = Paths.get("./java"); // create directory Files.createDirectories(path); }catch (IOException ex) { ex.printStackTrace(); } You can even use Files.createDirectories() to create multiple nested directories at once. This method creates all nonexistent parent directories first. Here is an example: try { // directory path Path path = Paths.get("./java/jvm/1.8"); // create directory Files.createDirectories(path); }catch (IOException ex) { ex.printStackTrace(); }
🌐
Jenkov
jenkov.com › tutorials › java-io › file.html
Java File
December 17, 2019 - You can use the Java File class to create directories if they don't already exists. The File class contains the method mkdir() and mkdirs() for that purpose. The mkdir() method creates a single directory if it does not already exist.