递归创建目录 [英] Recursively create directory
本文介绍了递归创建目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有人知道如何使用Java来创建基于n级深度字母(az)的子目录?
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
..
推荐答案
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
根据给定的 String
列表重新创建深度
-level目录树,其中, main
的情况,由英文字母表中的字符列表组成。
mkDirs
recusively creates a depth
-level directory tree based on a given list of String
s, which, in the case of main
, consists of a list of characters in the English alphabet.
这篇关于递归创建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文