16.03. Creating a directory in Java

Creating a directory in Java

  • Eclipse: Oxygen
  • Java: 1.8

Creating a directory in Java

In this tutorial, we will deal with the creation of the file directory, which has the same procedure as the creation of the file. Therefore the same concepts are shown as the absolute and relative location.

The following constructor takes a file object to create the directory. First, we create a file object using File() method as follows:

File directory = new File(“StudyEasy.txt”);

As in the previous example of creating a file in Java, we can create a directory in Java, the only difference is that we need to use the mkdir() method to create a directory in Java.

Here is the program that demonstrates creating a subfolder inside the directory. We use mkdirs() method instead of mkdir(). With mkdirs() you can create a file inside the directory of the leaf folder in your path, the path will be interpreted as relative to the current project.

Contributed by: Poonam Tomar

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments