2011-07-21 51 views
14

有誰知道如何使用Java創建基於n級深度的字母(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 
     .. 

回答

4
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創建一個基於的String秒的定列表上depth -level目錄樹,其中,在main的情況下,由英文字母表中的字符列表組成。

0

你可以使用三個環路上的字符a-z像這樣:

import java.io.*; 

public class FileCreate { 

    public static void main(String[] args) throws Exception { 
     char trunkDirectory = 'a'; 
     char branchDirectory = 'a'; 
     char leaf = 'a'; 

     while (trunkDirectory != '{') { 
      while (branchDirectory != '{') { 
       while (leaf != '{') { 
        System.out.println(trunkDirectory + "/" + branchDirectory + "/" + leaf++); 
       } 
       leaf = 'a'; 
       branchDirectory++; 
      } 
      branchDirectory = 'a'; 
      trunkDirectory++; 
     } 

    } 
} 

這只是輸出的路徑到控制檯。您可以使用File#mkdirs()創建遞歸目錄結構或在嵌套循環的每個中間部分創建目錄。我會讓你完成。

+0

天才!另一個qn:如果我想限制某個字母集合,該怎麼辦? – osley

+0

如果子集是連續的,那麼可以很容易地改變開始和結束字符。如果它是一組稀疏的字符,那麼最好創建一個字符數組,例如'char [] alphabet = {'a','e','i','o','u'};'並循環而不是索引像'for(int i = 0; i andyb

+0

感謝無法解釋的downvote 4.5年後 – andyb

0

我會寫一點實用方法,它將開始和結束字母以及所需的深度作爲參數。該方法遞歸調用自己,直到完成:

private static void createAlphabetFolders(File parent, int start, int end, int deepth){ 

    if(deepth <= 0){ 
     return; 
    } 

    for (int i=start; i < end; i++){ 

     // create the folder 
     String folderName = "" + ((char) i); 
     File folder = new File(parent, folderName); 
     System.out.println("creating: " + folder.getPath()); 
     folder.mkdirs(); 

     // call recursively 
     createAlphabetFolders(folder, start, end, deepth-1); 
    } 
    } 

人們會這樣稱呼它:

createAlphabetFolders(new File("abctest"), 'A', 'E', 5); 
10

如果您不介意依賴第三方API,Apache Commons IO包將直接爲您執行此操作。看看FileUtils.ForceMkdir

Apache許可證是商業軟件開發友好的,即它不要求您按照GPL的方式分發源代碼。 (根據你的觀點,這可能是好事或壞事)。

+0

迄今爲止最好的答案爲實際目的(我。即除非這是一項教育活動)。並強制「構建」問題:即使最簡單的應用程序也需要構建框架,以避免管理命令行類路徑,罐子位置等。每個程序員都應該儘早學習Gradle:基本使用並不困難。然後總是塞進(熟悉)Apache Commons ...從不重新發明輪子。 –

87

您可以簡單地使用java.io.File類的mkdirs()方法。

+6

或從Java 7開始的'Files.createDirectories' –

+1

一個例子可以讓答案看起來很好 –

-1
// ensures parent directory is created 
    File parentFile = destFile.getParentFile(); 
    if (parentFile != null && !parentFile.exists()) { 
     parentFile.mkdirs(); 
    } 

    // creates destination file 
    if (!destFile.exists()) { 
     destFile.createNewFile(); 
    } 
0

Groovy具有FileTreeBuilder類。問題是,它的描述很薄弱,例子有一個錯誤。所以,我還沒有看到使用它的任何代碼。正如我所發現的,如果不設置baseDir字段,它將無法正常工作。也許,它會解決你的問題。

def tree = new FileTreeBuilder() 
tree.src { 
    main { 
     groovy { 
      'Foo.groovy'('println "Hello"') 
     } 
    } 
    test { 
     groovy { 
      'FooTest.groovy'('class FooTest extends GroovyTestCase {}') 
     } 
    } 
} 

這是來自文檔的示例。但只有當你設置baseDir時,它纔會起作用。例如,通過傳遞構造函數參數。

0

Scala代碼:

def makePathRecursive(path: String) = { 
    import java.io.File 
    import scala.util.{Try, Failure, Success} 

    val pathObj = new File(path) 
    pathObj.exists match { 
     case true => // do nothing 
     case false => Try(pathObj.mkdirs) match { 
     case Success(_) => // it worked 
     case Failure(e) => // maybe created meanwhile by another thread 
      pathObj.exists match { 
      case false => throw new Exception(e) 
      case _ => 
     } 
     } 
    } 
    } 
-1

阿帕奇公地解決這些最。嘗試 -

org.apache.commons.io.FileUtils.forceMkdir(directory);

+2

我不認爲這是對問題的回答。 _這個單一方法是做什麼要求的?問題是關於算法,而不是關於使用什麼API。 – jdv

+0

@jdv你是怎麼確定這個問題是關於算法而不是API的,我在這個問題本身沒有看到任何這樣的描述。 – KMP

+0

「......如何......」但我的評論真的是關於一個外部API的唯一參考並不能提供一個好的答案。 – jdv