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.

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 to create 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


Full stack Java Developer

   Buy me a coffee!

 

PayTM QR (INDIA)

Download file

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu