Create directory in Java

In this tutorial, I will show you some ways to create directory in Java.

The first solution, we will use the File object.

This object allow us can create a specific directory or a specific directory with its child.

To create a specific directory, we will use the method mkdir() of File object. You can reference the code as below:

To create a specific directory with its child, we will use the method mkdirs():

The second solution, to create a new directory in Java, we can use the Files object.

This Files object was introduced since Java 7.

In this object, we have 2 static method: createDirectory() and createDirectories(). These methods will help us can create a specific directory (createDirectory() method) and its child (createDirectories() method).

Argument of these methods is a Paths object. This Paths object as calling name, it keep the information of the path of the directory. Then, these methods can use it to create directory.

Below is the code help us can create a specific directory and its child.



Add Comment