How to create a directory in Java?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Creating directories in Java is a common task that can be accomplished using various Java core libraries. Among these, java.io.File and java.nio.file packages are most frequently used due to their simplicity and powerful capabilities respectively. Below, we explore methods to create directories using both techniques, and compare their functionalities and use cases.
Using java.io.File
The java.io.File class provides a straightforward interface for file creation and directory operations. Here's how you can create a directory using this class:
The mkdir() method attempts to create the directory named by the abstract pathname. It returns true if and only if the directory was created; false otherwise.
It's important to note that mkdir() will only create the directory if its parent directories already exist. To create the target directory along with all necessary parent directories, you should use mkdirs() instead.
Using java.nio.file
With the introduction of NIO (New Input/Output) in Java 7, working with file and directory paths has become more flexible and robust through the java.nio.file package. One of the key classes in this package is Path, which replaces the File class for more complex operations. To create directories using NIO:
The Files.createDirectory(path) method throws an IOException if the directory cannot be created (e.g., if the parent directory does not exist). Like the File approach, there is also a Files.createDirectories(path) method to create the directory and all its parent directories.
Comparison and Features
| Feature/Functionality | java.io.File | java.nio.file Packages |
| Check if directory exists | boolean exists = file.exists(); | boolean exists = Files.exists(path); |
| Create single directory | file.mkdir(); | Files.createDirectory(path); |
| Create directory tree | file.mkdirs(); | Files.createDirectories(path); |
| Exception handling | No exception thrown on failure | Throws IOException on failure
(offers more control and error information) |
Error Handling
When using java.nio.file, error handling is more robust due to exception handling mechanisms that allow you to catch specific problems like AccessDeniedException, FileAlreadyExistsException, and others. This fine-grained control of exception handling delivers a significant advantage over the old java.io.File approach, where developers might not know the specific reason for the failure.
Conclusion
The use of java.nio.file is preferred in modern Java applications due to its extensive functionality and control over file system operations, including error handling and the ability to create entire directory trees with one command. However, for quick and simple directory creation, java.io.File can still be quite effective. When deciding which method to use, consider the complexity of your requirement and the level of control you need over file system operations.
Related reading
- How to create a file in a directory in java?
- How to create a .jar file or export JAR in IntelliJ IDEA (like Eclipse Java archive export)?
- How to create a Java Date object of midnight today and midnight tomorrow?
- How to create a Looper thread, then send it a message immediately?
- How to create a project from existing source in Eclipse and then find it?
- How to create a release signed apk file using Gradle?
- How to create a String with format?
- How to create a sub array from another array in Java?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.