Create directories recursively in Java

Java 8Object Oriented ProgrammingProgramming

The method java.io.File.mkdirs() is used to create the specified directories, including the necessary parent directories. This method requires no parameters and it returns true on the success of the directories creation or false otherwise.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.io.File;
public class Demo {
   public static void main(String[] args) {
      String recursiveDirectories = "D:\\a\\b\\c\\d";
      File file = new File(recursiveDirectories);
      boolean flag = file.mkdirs();
      System.out.println("The directories are created recursively? " + flag);
   }
}

The output of the above program is as follows −

Output

The directories are created recursively? true

Now let us understand the above program.

The method java.io.File.mkdirs() is used to create the specified directories recursively. The boolean value returned by the method is printed. A code snippet that demonstrates this is given as follows −

String recursiveDirectories = "D:\\a\\b\\c\\d";
File file = new File(recursiveDirectories);
boolean flag = file.mkdirs();
System.out.println("The directories are created recursively? " + flag);
raja
Published on 25-Feb-2019 12:08:00
Advertisements