'Creating directories in Java

enter image description hereI'm very new to the Java world so please forgive my ignorance.

What is the optimal way to create 1000 New directories in Java?

knowing that I have a specific number for each new directory for example ( Create D\NEW_Directories\DIR101234...DIR107601...DIR108234... to DIR#1000.

I already have the specific 1000 numbers list that I want to plug in in a code to create the new 100 empty directories for them. I found a couple of examples on how to create a single directory but not multiple ones. I'm using Eclipse Marse 2 in a Win64 environment.



Solution 1:[1]

In my example i use "i" like suffix , and i check if direcory exists.

The solution doesn't depend of the IDE you are using , everything used in here it is included in the java standard libraries.

String OUTPUT_FOLDER = "pathwhereyouwantcreatefolders";
        for(int i = 1 ;i<5;i++){
            File folder = new File(OUTPUT_FOLDER+"_"+i);
            if(!folder.exists()){
                folder.mkdir();
            }
            }//for

Solution 2:[2]

If I have anderstood good your question, here is the code example (just change 10 with 1000).

import java.io.File;

public class Directories {

    public static void main(String[] args) {
        //We are creating 10 directories in a parent directory called NEW_DIRECTORIES
        boolean new_dir = new File("NEW_DIRECTORIES").mkdir();
        boolean successCreation;
        if (new_dir) {
            for (int i = 1; i < 11; i++) {
                do {
                    int folderName = (int) (Math.random() * 899999) + 100000; //Give a random number from 100000 to 999999
                    String aDirName = "NEW_DIRECTORIES/" + folderName;
                    successCreation = new File(aDirName).mkdir();
                } while (!successCreation); //We need this condition to make sure that a number has not been chosen twice
            }
        }
    }
}

And the output should be like this (TESTED).

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Frank
Solution 2