You can simply use the mkdirs() method of java.io.File class.

Example:

new File("C:\\Directory1\\Directory2").mkdirs();
Answer from Zhile Zou on Stack Overflow
🌐
GitHub
gist.github.com › barbietunnie › a9b63e58d806859bba816862b8f101b3
Create recursive directory using Java NIO · GitHub
Create recursive directory using Java NIO. GitHub Gist: instantly share code, notes, and snippets.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-create-directories-recursively
Java Program to Create Directories Recursively - GeeksforGeeks
July 23, 2025 - If the Directory is not present, open a try-catch block, and create a new directory using createDirectories() method. Else, display that the Directory already exists. ... Below is the implementation of the above program.
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › javaexamples › dir_create.htm
How to create directories recursively in Java
Following example shows how to create directories recursively with the help of file.mkdirs() methods of File class. import java.io.File; public class Main { public static void main(String[] args) { String directories = "D:\\a\\b\\c\\d\\e\\f\\g\\h\\i"; File file = new File(directories); boolean result = file.mkdirs(); System.out.println("Status = " + result); } }
🌐
Kodejava
kodejava.org › how-do-i-create-a-directories-recursively
How do I create a directories recursively? - Learn Java by Examples
May 17, 2023 - String directories = "D:/kodejava/a/b/c/d/e/f/g/h/i"; File file = new File(directories); // The mkdirs will create folder including any necessary // but nonexistence parent directories.
Find elsewhere
🌐
Mkyong
mkyong.com › home › java › how to create directory in java
How to create directory in Java - Mkyong.com
July 29, 2020 - If the parent directories not exist, create it first. If the directory exists, no exception thrown. ... 1.3 This example uses Files.createDirectories to create a directory /test4/ including all nonexistent parent directories /test2/test3/. ... ...
🌐
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.
🌐
W3Docs
w3docs.com › java
Create a directory if it does not exist and then create the files in that directory as well
import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class Main { public static void main(String[] args) throws Exception { String dirName = "mydir"; Path dirPath = Paths.get(dirName); if (!Files.exists(dirPath)) { Files.createDirectory(dirPath); } String fileName = "myfile.txt"; Path filePath = dirPath.resolve(fileName); if (!Files.exists(filePath)) { Files.createFile(filePath); } } }
🌐
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.
🌐
ZetCode
zetcode.com › java › createdirectory
Java create directory - learn how to create a directory in Java
The Files.createDirectories creates a new directory; if the parent directories do not exist, they are created as well. The method does not thrown an exception if the directory already exist. JavaCreateDirectories.java ·
🌐
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.
🌐
O'Reilly
oreilly.com › library › view › java-cookbook › 0596001703 › ch10s10.html
Making New Directories - Java Cookbook [Book]
June 21, 2001 - Use java.io.File’s mkdir( ) or mkdirs( ) method. Of the two methods used for creating directories, mkdir( ) creates just one directory while mkdirs( ) creates any parent directories that are needed.
Author   Ian F. Darwin
Published   2001
Pages   888
🌐
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 ...
🌐
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 not only take care of releasing resources once you are done with that, but also removes coding headache and finally block from above code, making it more concise and readable. 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()) { ...