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
🌐
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.
🌐
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.
🌐
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 ... dirName = "mydir"; Path dirPath = Paths.get(dirName); if (!Files.exists(dirPath)) { Files.createDirectory(dirPath); } } } ... This code creates a Path object representing the directory ...
🌐
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.
🌐
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.
🌐
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.
🌐
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. ... package com.zetcode; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; ...
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()) { ...
🌐
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 ... + directoryName; // Create a File object representing the directory File directory = new File(directoryPath); // Attempt to create the directory boolean directoryCreated = directory.mkdir(); ...
🌐
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 ...
🌐
How to do in Java
howtodoinjava.com › home › i/o › creating new directories in java
Creating New Directories in Java
April 21, 2022 - We will learn to create the archive directory in such a way: ... The createDirectory() creates the new directory if all the parent directories exist. The check for the existence of the file and ...
🌐
Stack Overflow
stackoverflow.com › questions › 39975615 › java-creating-file-or-directory-if-not-existent
Java: Creating File or Directory if not existent - Stack Overflow
April 6, 2017 - public SerializedFahrzeugDAO(String path) { pfad=path; try{ FileInputStream filein= new FileInputStream(pfad); ObjectInputStream in = new ObjectInputStream(filein); List<Fahrzeug> liste = (List<Fahrzeug>)in.readObject(); in.close(); FileOutputStream fileout = new FileOutputStream(pfad); ObjectOutputStream out = new ObjectOutputStream(fileout); out.writeObject(liste); out.close(); } catch (Exception f){ if(f instanceof FileNotFoundException){ try{ File ziel = new File(pfad); File dir= new File(ziel.getParentFile().getAbsolutePath()); dir.mkdirs(); FileOutputStream fileout = new FileOutputStream(pfad); ObjectOutputStream out = new ObjectOutputStream(fileout); List<Fahrzeug> newlist = new ArrayList<Fahrzeug>(); out.writeObject(newlist); out.close(); } catch(Exception y){System.out.println(y); } } else {System.out.println(f);}}
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › nio › file › Files.html
Files (Java Platform SE 8 )
April 21, 2026 - NotDirectoryException - if the file could not otherwise be opened because it is not a directory (optional specific exception) ... SecurityException - In the case of the default provider, and a security manager is installed, the checkRead method is invoked to check read access to the directory. public static Path createFile(Path path, FileAttribute<?>... attrs) throws IOException · Creates a new and empty file, failing if the file already exists.
🌐
ZetCode
zetcode.com › java › createfile
Java create file - learn how to create a file in Java
The tutorials shows five ways to create a file in Java. The examples create empty files. The File's createNewFile method creates a new, empty file named by the pathname if a file with this name does not yet exist.
🌐
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