You can ensure that parent directories exist by using this method File#mkdirs().

File f = new File("D:\\test3\\ts435\\te\\util.log");
f.getParentFile().mkdirs();
// ...

If parent directories don't exist then it will create them.

Answer from Mushif Ali Nawaz on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › io › dirs.html
Creating and Reading Directories (The Java™ Tutorials > Essential Java Classes > Basic I/O)
Files.createDirectories(Paths.get("foo/bar/test")); The directories are created, as needed, from the top down. In the foo/bar/test example, if the foo directory does not exist, it is created. Next, the bar directory is created, if needed, and, finally, the test directory is created. It is possible for this method to fail after creating some, but not all, of the parent directories.
🌐
Mkyong
mkyong.com › home › java › how to create directory in java
How to create directory in Java - Mkyong.com
July 29, 2020 - try { Path path = Paths.get("/....println("Failed to create directory!" + e.getMessage()); } 1.1 We can use Files.createDirectory to create a directory. If the parent directories not exist, throws NoSuchFileExcepti...
🌐
Baeldung
baeldung.com › home › java › java io › create a directory in java
Create a Directory in Java | Baeldung
January 8, 2026 - 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. Note that until now we used the File(File, String) constructor, but we can also use the File(String) constructor and pass the complete path of our file using File.separator to separate the different parts of the path: File newDirectory = new File(System.getProperty("java.io.tmpdir") + File.separator + "new_directory"); File nestedDirectory = new File(newDirectory, "nested_directory"); assertFalse(newDirectory.exists()); assertFalse(nestedDirectory.exists()); assertTrue(nestedDirectories.mkdirs());
🌐
Medium
medium.com › @AlexanderObregon › javas-files-createdirectories-method-explained-cb824678b0b7
Java’s Files.createDirectories() Method Explained | Medium
January 26, 2025 - The createDirectories() method in the java.nio.file.Files class allows you to create directories along with any missing parent directories. It's particularly useful when you need to set up nested folder structures for your application.
🌐
How to do in Java
howtodoinjava.com › home › i/o › creating new directories in java
Creating New Directories in Java
April 21, 2022 - File newDir = new File("c:/temp/data/archive"); boolean isDirectoryCreated = newDir.mkdirs(); In this Java tutorial, we learned to create a new single directory as well as a nested directory along with its all parent directories.
🌐
Simplesolution
simplesolution.dev › java-create-parent-and-sub-directories-using-files.createdirectories
Java Create Parent and Sub Directories using Files.createDirectories()
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class FilesCreateDirectoriesExample1 { public static void main(String... args) throws IOException { Path directoryToCreate = Paths.get("D:\\SimpleSolution\\Data\\Java\\JavaNIO"); Path createdDirectories = Files.createDirectories(directoryToCreate); System.out.println("Successful create directories: " + createdDirectories.toString()); } } The output as below.
🌐
ZetCode
zetcode.com › java › createdirectory
Java create directory - learn how to create a directory in Java
... We first check if the directory does not already exist with Files.exists. ... The directory is created with Files.createDirectory. The method takes a path object as a parameter. The Files.createDirectories creates a new directory; if the parent directories do not exist, they are created as well.
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
On the other hand, creating file and directories are simple in Java, as java.io.File provides methods like createNewFile() and mkdir() to create new file and directory in Java. These methods returns boolean, which is the result of that operation ...
🌐
Delft Stack
delftstack.com › home › howto › java › create directory java
How to Create Directory in Java | Delft Stack
February 2, 2024 - " + fileName); } } Output: New Directory created ! C:\NonExisting1\NonExisting2 · The org.apache.commons.io.FileUtils package has the forceMkdir() function, creating directories and parent directories if required.
🌐
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.
🌐
Grabthiscode
grabthiscode.com › java › java-create-file-and-parent-directories
Java create file and parent directories - code example - GrabThisCode.com
Get code examples like"java create file and parent directories". Write more code and save time using our ready-made code examples.
🌐
IQCode
iqcode.com › code › java › java-create-file-and-parent-directories
java create file and parent directories Code Example
if(!Files.exists(Path.of([PATH]))) { File f = new File([PATH]); f.getParentFile().mkdirs(); }
🌐
CodingTechRoom
codingtechroom.com › question › -create-file-in-java-with-all-parent-directories
How to Create a File in Java Along with All Parent Directories - CodingTechRoom
Learn how to create a file in Java while ensuring all parent directories are created using Java NIO.2. Step-by-step guide with code examples.
🌐
Java Development Journal
javadevjournal.com › home › how to create directory in java
How to create directory in Java | Java Development Journal
June 14, 2017 - The method will not throw an exception if the directory could not be created because it already exists.In case it fails, it may do so after creating some, but not all, of the parent directories. Both these methods will delegate work to FileSystemProvider. FileAttribute attrs... is an optional parameter which can be used to set file attributes while creating file /directory. For most of the case, FileAttribute will be set automatically. Apache Commons FileUtils#forceMkdir provide a convenient and easy way to create a directory in Java.
🌐
JDriven
jdriven.com › blog › 2021 › 10 › Clojure-Goodness-Create-All-Parent-Directories-For-A-File
Clojure Goodness: Create All Parent Directories For A File - JDriven Blog
October 12, 2021 - (ns mrhaki.io.make-parents (:require [clojure.java.io :refer [make-parents file]] [clojure.test :refer [is]])) ;; make-parents will create the parents directories for a file ;; The function returns true if the directories are created, ;; false if the directories already exist.