Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursively create directory

Does anyone know how to use Java to create sub-directories based on the alphabets (a-z) that is n levels deep?

 /a
    /a
        /a
        /b
        /c
        ..
    /b
        /a
        /b
        ..
    ..
        /a
        /b
        /c
        ..

/b
    /a
        /a
        /b
        ..
    /b
        /a
        /b
        ..
    ..
        /a
        /b
        ..
..
    /a
        /a
        /b
        ..
    /b
        /a
        /b
        ..
    ..
        /a
        /b
        ..
like image 840
osley Avatar asked Jul 21 '11 10:07

osley


People also ask

How do I create a recursive directory?

The -p option is used to create multiple child directories with mkdir command in a recursive manner. In order to create directories recursively non of the specified directories should exist because all of them are created automatically.

How do you create a recursive directory in Java?

Create an object of the File class with md as a parameter. Check if the directory already exists using the exists() method and display the message. Else create the directory using the mkdir()method. Make the recursive call.

How do I create a recursive directory in Python?

makedirs() method in Python is used to create a directory recursively. That means while making leaf directory if any intermediate-level directory is missing, os. makedirs() method will create them all. Suppose we want to create directory 'ihritik' but Directory 'GeeksForGeeks' and 'Authors' are unavailable in the path.

What is directory recursion?

Alternatively referred to as recursive, recurse is a term used to describe the procedure capable of being repeated. For example, when listing files in a Windows command prompt, you can use the dir /s command to recursively list all files in the current directory and any subdirectories.


4 Answers

You can simply use the mkdirs() method of java.io.File class.

Example:

new File("C:\\Directory1\\Directory2").mkdirs();
like image 79
Zhile Zou Avatar answered Oct 23 '22 10:10

Zhile Zou


If you don't mind relying on a 3rd party API, the Apache Commons IO package does this directly for you. Take a look at FileUtils.ForceMkdir.

The Apache license is commercial software development friendly, i.e. it doesn't require you to distribute your source code the way the GPL does. (Which may be a good or a bad thing, depending on your point of view).

like image 42
Rex Avatar answered Oct 23 '22 11:10

Rex


Since Java 7, java.nio is preferred

import java.nio.file.Files;
import java.nio.file.Paths;

...

Files.createDirectories(Paths.get("a/b/c"));
like image 7
glorat Avatar answered Oct 23 '22 10:10

glorat


public static void main(String[] args) {
  File root = new File("C:\\SO");
  List<String> alphabet = new ArrayList<String>();
  for (int i = 0; i < 26; i++) {
    alphabet.add(String.valueOf((char)('a' + i)));
  }

  final int depth = 3;
  mkDirs(root, alphabet, depth);
}

public static void mkDirs(File root, List<String> dirs, int depth) {
  if (depth == 0) return;
  for (String s : dirs) {
    File subdir = new File(root, s);
    subdir.mkdir();
    mkDirs(subdir, dirs, depth - 1);
  }
}

mkDirs recusively creates a depth-level directory tree based on a given list of Strings, which, in the case of main, consists of a list of characters in the English alphabet.

like image 2
João Silva Avatar answered Oct 23 '22 11:10

João Silva