Java – Create directory recursively

Does anyone know how to use java to create subdirectories based on N-level depth letters (A-Z)?

/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
        ..

Solution

public static void main(String[] args) {
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 recreates a depth level directory tree based on the given string list. In the case of the main list, it contains a list of characters in the English alphabet

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>