Java create directory example shows how to create a directory in Java. The example also shows how to create directories including all parent directories or folders.
How to create a directory in Java?
Java File class provides two methods mkdir
and mkdirs
which can be used to create directories.
1 |
public boolean mkdir() |
This method creates a directory denoted by the file object. It returns true if the directory was created successfully, false otherwise.
Java Create Directory Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.javacodeexamples.ioexamples; import java.io.File; public class CreateDirectoryExample { public static void main(String[] args) { //Path of the directory String strPath = "C:/dir_1"; //Create File object from path File dir = new File(strPath); /* * To create directory or folder, use mkdir * method of File class. */ boolean isDirCreated = dir.mkdir(); if(isDirCreated) System.out.println("Directory created successfully"); else System.out.println("Failed to create directory"); } } |
Output
1 |
Directory created successfully |
If you re-run the same program, the output will be “Failed to create directory” because the directory was already created in the first run.
How to create all required directories including non-existent parent directories?
Consider the following code.
1 2 3 4 5 6 7 8 9 |
String strPath = "C:/dir_1/dir_2"; File dir = new File(strPath); boolean isDirCreated = dir.mkdir(); if(isDirCreated) System.out.println("Directory created successfully"); else System.out.println("Failed to create directory"); |
If dir_1 does not exist, the above-given code will fail to create the dir_2. If you want to create all directories including non-existent parent directories, use the mkdirs
method instead of the mkdir
method.
1 |
public boolean mkdirs() |
Example
1 2 3 4 5 6 7 8 9 |
String strPath = "C:/dir_1/dir_2"; File dir = new File(strPath); boolean isDirCreated = dir.mkdirs(); if(isDirCreated) System.out.println("Directories created successfully"); else System.out.println("Failed to create directories"); |
Output
1 |
Directories created successfully |
Is it necessary to check if a directory exists before creating it?
You might think that it would be a good idea to check if the directory exists before actually creating it using the exists
method like given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
String strPath = "C:/dir_1/dir_2"; File dir = new File(strPath); if(!dir.exists()){ boolean isDirCreated = dir.mkdir(); if(isDirCreated) System.out.println("Directory created successfully"); else System.out.println("Failed to create directory"); } |
The above-given code calls the mkdir
method only if the directory does not exist. Here a call to the exists
method is not necessary because mkdir
returns true if and only if the directory was created successfully. So even if the directory exists, the mkdir
method will not cause any harm neither it will throw any exceptions.
How to create directories using Java 7 NIO?
If you are using JDK 7 or a later version, you can use Paths and Files classes to create directories as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.javacodeexamples.ioexamples; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class CreateDirectoryExample { public static void main(String[] args) { //Path of the directory Path dirPath = Paths.get("C:/dir_1"); /* * Use createDirectory method of Files class * to create a directory */ try { Files.createDirectory(dirPath); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } /* * Use createDirectories method of Files class * to create a directory along with all the * non-existent parent directories */ //Path of the directory Path dirsPath = Paths.get("C:/dir_1/dir_2"); try { Files.createDirectories(dirsPath); } catch (IOException e) { e.printStackTrace(); } } } |
Apache Commons User?
Have a look at the forceMkdir
method of the Apache Commons FileUtils class.
1 |
public static void forceMkdir(File dir) throws IOException |
This method creates the specified directory along with all the non-existent parent directories. This method may throw IOException
in case of failure.
This example is a part of Java File tutorial with examples.
Please let me know your views in the comments section