Let’s learn how to create directory in java.
How to create directory in java
There are two methods to create directory. They are mkdir() and mkdirs() of class File which returns boolean value.

File mkdir() method creates the directory named by this abstract pathname. File mkdir() method returns true if and only if the directory created; false otherwise.
Now let’s see File mkdir() method in java example.
import java.io.File; public class CreateDirectory { public static void main(String[] args) { File directory = new File("D:\\DirectoryExample"); if(!directory.exists()) { if(directory.mkdir()) { System.out.println("Directory is created"); } else { System.out.println("Directory not created"); } } } }
Output:
Directory created
File mkdirs() method in java creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.
File mkdirs() method returns true if and only if the directory was created, along with all necessary parent directories; false otherwise.
Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.
Here’s the File mkdirs() method in java example.
public class CreateDirectoryDemo { public static void main(String[] args) { File directory = new File("D:\\DirectoryExample\\subdirectory1\\subdirectory2"); if(!directory.exists()) { if(directory.mkdirs()) { System.out.println("Sub directories created"); } else { System.out.println("Sub directories not created"); } } } }
Output:
Sub directories created
NOTE: make directories method(mkdirs) and make directory method(mkdir) in java both return boolean value to show program status.
Java NIO
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class NioDirectoryExample { public static void main(String[] args) { Path path = Paths.get("D:\\Directory1\\Subdirectory1\\SubSubdirectory2"); // this will check if directory exists? if(!Files.exists(path)) { try { Files.createDirectories(path); } catch(IOException ioe) { // if create directory fails to execute ioe.printStackTrace(); } } } }
Also read – major features of java