How to create a directory hierarchy using Java?



The class named File of the java.io package represents a file or directory (path names) in the system. This class provides various methods to perform various operations on files/directories.

The mkdir() method of this class creates a directory with the path represented by the current object.

Creating directory hierarchy

There are two main ways to create a directory hierarchy in Java −

  • Using mkdirs() Method
  • Using createDirectories() Method

Let us look at these solutions one by one.

Using mkdirs() Method

To create a hierarchy of new directories you can using the method mkdirs() of the same class. This method creates the directory with the path represented by the current object, including non-existing parent directories.

Example

 import java.io.File; import java.util.Scanner; public class CreateDirectory {    public static void main(String args[]) {       System.out.println("Enter the path to create a directory: ");       Scanner sc = new Scanner(System.in);       String path = sc.next();       System.out.println("Enter the name of the desired a directory: ");       path = path+sc.next();       //Creating a File object       File file = new File(path);       //Creating the directory       boolean bool = file.mkdirs();       if(bool) {          System.out.println("Directory created successfully");       }else {          System.out.println("Sorry couldn't create specified directory");       }    } }

Output

Enter the path to create a directory: D:\test\myDirectories\ Enter the name of the desired a directory: sample_directory Directory created successfully

If you verify you can observe see the created directory as −

Using the createDirectories() Method

Since Java 7 the Files class was introduced this contains (static) methods that operate on files, directories, or other types of files.

The createDirectories() method creates the given directory including the non-existing parent directory.

Example

 import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Scanner; public class Demo {    public static void main(String args[]) throws IOException {       System.out.println("Enter the path to create a directory: ");       Scanner sc = new Scanner(System.in);       String pathStr = sc.next();             System.out.println("Enter the required directory hierarchy: ");       pathStr = pathStr+sc.next();             //Creating a path object       Path path = Paths.get(pathStr);             //Creating a directory       Files.createDirectories(path);             System.out.println("Directory hierarchy created successfully");    } }

Output

 Enter the path to create a directory: D: Enter the required directory hierarchy: sample1/sample2/sapmle3/final_directory Directory hierarchy created successfully

If you verify, you can observe see the created directory hierarchy as −

Updated on: 2024-03-12T18:37:31+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements