How to create directories recursively in java?

To create directories recursively, we will use file.mkdirs() method. The mkdirs() method is a part of the File class. The mkdirs() function is used to create a new directory denoted by the abstract pathname and also creates all the non-existent parent directories of the abstract pathname. If the mkdirs() function fails to create some directory it might have created some of its parent directories. The function returns true if the directory is created otherwise returns false.

Below java code shows how to create directories recursively using the file.mkdirs() java method –

/****************************************************************************************
* Created on 03-2012 Copyright(c) https://kodehelp.com All Rights Reserved.
****************************************************************************************/
package com.kodehelp.javaio;

import java.io.File;

/**
 * Created by https://kodehelp.com Date: 03/05/2012
 */
public class RecursiveDirectoryCreation {

    public static void main(String[] args) {
        String directories = "C:\\001\\002\\003\\004\\005\\006\\007\\008\\009";
        File file = new File(directories);

        boolean passFail = file.mkdirs();
        System.out.println("Directory Creation Status = " + passFail);

    }

}
References: