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 OverflowVideos
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.
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); } }
Top answer 1 of 4
121
You have to actually call some method to create the directories. Just creating a file object will not create the corresponding file or directory on the file system.
You can use File#mkdirs() method to create the directory: -
theFile.mkdirs();
Difference between File#mkdir() and File#mkdirs() is that, the later will create any intermediate directory if it does not exist.
2 of 4
20
Use this code spinet for create intermediate folders if one doesn't exist while creating/editing file:
File outFile = new File("/dir1/dir2/dir3/test.file");
outFile.getParentFile().mkdirs();
outFile.createNewFile();
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/. ... ...
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 ...
Top answer 1 of 3
18
Thanks to Apache this is super easy.
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class DeleteFolder {
public static void main(String[] args){
try {
File f = new File("/var/www/html/testFolder1");
FileUtils.cleanDirectory(f); //clean out directory (this is optional -- but good know)
FileUtils.forceDelete(f); //delete directory
FileUtils.forceMkdir(f); //create directory
} catch (IOException e) {
e.printStackTrace();
}
}
}
2 of 3
11
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
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()) { ...