Create a Directory in Java
This post will discuss how to create a directory in Java, including all non-existent parent directories.
1. Using File#mkdirs() method
The standard solution to create a directory is using the mkdir() method from the File class. It returns true if the directory is created; false otherwise. The invocation for this method would look like below:
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.io.File; public class Main { public static void main(String[] args) { File directory = new File("/path/dir"); if (directory.mkdir()) { System.out.println("Directory created successfully"); } } } |
Note that the mkdir() method is useful for creating a single directory. If you want to create a folder hierarchy, you should use the mkdirs() method instead. It creates the directory named by the abstract pathname, including any non-existent parent directories.
For example, the following code creates the parent directory named path (if non-existent), followed by its subdirectory dir.
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.io.File; public class Main { public static void main(String[] args) { File directory = new File("/path/dir"); if (directory.mkdirs()) { System.out.println("Directory created successfully"); } } } |
2. Using Files.createDirectories() method
With Java 7, you can use the createDirectory() method of the Files class to create a new directory. It throws FileAlreadyExistsException if the directory already exists and IOException if the parent directory does not exist.
|
1 2 3 4 5 6 7 8 9 10 |
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class Main { public static void main(String[] args) throws IOException { String path = "/path/dir"; Files.createDirectory(Paths.get(path)); } } |
With Java 8, you should use the Files.createDirectories() method that creates a new directory by creating all non-existent parent directories first. This method does not throw an exception if the directory already exists.
|
1 2 3 4 5 6 7 8 9 10 |
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class Main { public static void main(String[] args) throws IOException { String path = "/path/dir"; Files.createDirectories(Paths.get(path)); } } |
3. Using FileUtils.forceMkdir() method
Apache Commons IO also provides several utility methods for working with files. To construct a directory, including any necessary but non-existent parent directories, you can use the FileUtils.forceMkdir() method. It throws IOException if the file already exists but is not a directory.
|
1 2 3 4 5 6 7 8 9 10 11 |
import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { String path = "/path/dir"; FileUtils.forceMkdir(new File(path)); } } |
That’s all about creating a directory in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)