Recursively create directory

前端 未结 10 643
萌比男神i
萌比男神i 2020-12-30 23:20

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
           


        
10条回答
  •  悲&欢浪女
    2020-12-30 23:34

    public static void main(String[] args) {
      File root = new File("C:\\SO");
      List alphabet = new ArrayList();
      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 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.

提交回复
热议问题