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
🌐
LabEx
labex.io › tutorials › java-check-if-directory-exists-117392
Check if Directory Exists in Java | LabEx
You can use the File class in Java to check if a specified directory exists. You can use the isDirectory() method on a File object to check if it represents a directory. Here's an example: try { String path = "path/to/directory/"; File file ...
🌐
Baeldung
baeldung.com › home › java › java io › check if a file or directory exists in java
Check If a File or Directory Exists in Java | Baeldung
January 27, 2024 - Then we can pass that Path to the Files.exists(Path) method: Path path = Paths.get("does-not-exist.txt"); assertFalse(Files.exists(path)); Since the file doesn’t exist, it returns false.
🌐
Educative
educative.io › answers › how-to-check-if-a-file-or-folder-exists-in-java
How to check if a file or folder exists in Java
import java.nio.file.Paths; class Main { public static void checkFilePresent(Path path){ if (Files.exists(path)) { if (Files.isDirectory(path)) { System.out.println("It is a directory"); } else if (Files.isRegularFile(path)) { System.out.println("File test.txt present"); } } else { System.out.println("File not found "); } } public static void main( String args[] ) { String currentDir = "./"; String fileName1 = "test.txt"; Path path = Paths.get(currentDir + fileName1); checkFilePresent(path); String fileName2 = "write.txt"; path = Paths.get(currentDir + fileName2); checkFilePresent(path); } } Run ·
🌐
W3Docs
w3docs.com › java
How to check if a folder exists? | W3Docs
import java.nio.file.Files; import ... does not exist."); } } } This code creates a Path object representing the directory and then checks if it exists using the exists method....
🌐
Medium
medium.com › @AlexanderObregon › javas-files-exists-method-explained-11a2f15fe927
Java’s Files.exists() Method Explained | Medium
January 29, 2025 - Path userPath = Paths.get("user-provided-path"); if (Files.exists(userPath)) { if (Files.isRegularFile(userPath)) { System.out.println("The path points to a file."); } else if (Files.isDirectory(userPath)) { System.out.println("The path points to a directory."); } else { System.out.println("The path exists but is neither a file nor a directory."); } } else { System.out.println("The path does not exist."); } ... The program first checks if the path exists.
Find elsewhere
🌐
CalliCoder
callicoder.com › java-check-file-directory-exists
How to check if a File or Directory exists in Java | CalliCoder
February 18, 2022 - import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class CheckFileExists { public static void main(String[] args) { Path filePath = Paths.get("/Users/callicoder/demo.txt"); // Checking existence using Files.exists if(Files.exists(filePath)) { System.out.printf("File %s exists%n", filePath); } else { System.out.printf("File %s doesn't exist%n", filePath); } // Checking existence using Files.notExists if(Files.notExists(filePath)) { System.out.printf("File %s doesn't exist%n", filePath); } else { System.out.printf("File %s exists%n", filePath); } } }
🌐
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.
🌐
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); } } } Copy · This code creates a Path object representing the directory and then checks if it exists using the exists method.
🌐
w3resource
w3resource.com › java-exercises › io › java-io-exercise-3.php
Java - Check if a file or directory by pathname exists or not
import java.io.File; public class Exercise3 { public static void main(String[] args) { // Create a File object File my_file_dir = new File("/home/students/xyz.txt"); if (my_file_dir.exists()) { System.out.println("The directory or file exists.\n"); } else { System.out.println("The directory or file does not exist.\n"); } } } Sample Output: The directory or file does not exist.
🌐
Techndeck
techndeck.com › java-code-to-check-if-folder-or-directory-exists-or-not
Java code to check if folder (or directory) exists or not - Techndeck
April 13, 2022 - Once the folder has been created, we are going to use ‘exists‘ and ‘isDirectory’ method of Java-IO library. They will test whether the folder is present or not and if it’s a directory or not, based on the output, it will throw ‘pass‘ or ‘fail‘ in return. ... Now, by using below line of code, we are deleting the created folder and performing the folder existence check again.
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › io › check.html
Checking a File or Directory (The Java™ Tutorials > Essential Java Classes > Basic I/O)
The file is verified to not exist. The file's status is unknown. This result can occur when the program does not have access to the file. If both exists and notExists return false, the existence of the file cannot be verified.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-check-a-file-or-directory-exists-in-java
How to Check a File or Directory Exists in Java? - GeeksforGeeks
April 28, 2025 - // Importing required libraries import java.io.*; public class GFG { public static void main(String[] args) throws IOException { // Creating object of file and providing it path File f = new File("C:\\Test\\GFG.txt"); // Check if the specified file // Exists or not if (f.exists()) System.out.println("Exists"); else System.out.println("Does not Exists"); } } The above code sample will produce the following result (if the file "java.txt" exists in 'C:/Test' drive).
🌐
How to do in Java
howtodoinjava.com › home › i/o › check if a file or directory exists in java
Check If a File or Directory Exists in Java
November 14, 2022 - Java NIO provides a few good ways to test whether the specified file or directory exists or not. Use Files.exists() method or Files.notExists() method for such validations. Path path = Files.createTempFile("testFile", ".txt"); boolean exists ...
🌐
Attacomsian
attacomsian.com › blog › java-check-if-directory-exists
How to check if a directory exists in Java
December 12, 2019 - In Java 7 and higher, you can use the NIO API Files.isDirectory() static method to check if a folder exists in your file system: if (Files.isDirectory(Paths.get("/var/lib/"))) { System.out.println("Directory found."); } else { System.out.pr...
🌐
LabEx
labex.io › tutorials › java-check-if-a-file-exists-117390
Check if a File Exists in Java | LabEx
boolean isFile = file.isFile(); if(!isFile){ System.out.println("It is not a file"); }else{ System.out.println("It is a file"); } This code checks if the existing file is a file or a directory.
🌐
ZetCode
zetcode.com › java › createdirectory
Java create directory - learn how to create a directory in Java
package com.zetcode; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class JavaCreateDirectory { public static void main(String[] args) throws IOException { String fileName = "/home/janbodnar/tmp/newdir"; Path path = Paths.get(fileName); if (!Files.exists(path)) { Files.createDirectory(path); System.out.println("Directory created"); } else { System.out.println("Directory already exists"); } } } The example creates a new directory with Files.createDirectory. String fileName = "/home/janbodnar/tmp/newdir"; Path path = Paths.get(fileName); 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.