2017-07-19 203 views
-1

创建的目录,所以我的问题是,我想有我目录循环是这样的: https://gyazo.com/74209ec6e199adc3cd84460f7e0d5c2e爪哇 - 循环由数量

我的代码创建显示目录的:

public static File createDir(String path, String name) { 
    File dir = new File(path + "\\" + name); 
    dir.mkdir(); 
    return dir; 

} 

public static void createDirs(String path, int times) { 
    int x; 
    for(x=1; x < times+1; x++){ 
     Utils.createDir(path+File.separator, Integer.toString(x)); 
    } 

} 

public static void main(String[] args){ 
    Utils.createDir(System.getProperties().getProperty("user.home")+File.separator+"Desktop", "Dir"); 
    createDirs(System.getProperties().getProperty("user.home")+File.separator+"Desktop"+File.separator+"Dir", 10); 
} 

}

但我不知道如何制作它。任何帮助表示赞赏。

编辑:现在看起来是这样的权利: https://gyazo.com/e4877b87c6d9e1910bad7849daafd431

回答

0

您需要修改createDirs

public static void createDirs(String path, int times) { 
    int x; 
    for(x=1; x < times+1; x++){ 
     path += File.separator + Integer.toString(x); 
     new File(path).mkdir(); 
    } 
} 
+0

谢谢,它对我很好。 –

0

@Zain Drozal的环内的path变量,这是修改后的代码,你到底想要

package stack; 

import java.io.File; 

public class Utils { 
    static File lastDir; 

    public static File createDir(String path, String name) { 
    File dir = new File(path + "\\" + name); 
    dir.mkdir(); 
    return dir; 
    } 

    public static void createDirs(String path, int times) { 
    int x; 
    for(x=1; x < times+1; x++){ 
    lastDir = Utils.createDir(lastDir.getPath()+File.separator, Integer.toString(x)); 
    } 
    } 

    public static void main(String[] args) { 
    lastDir = Utils.createDir(System.getProperties().getProperty("user.home")+File.separator+"Desktop", "Dir"); 
    createDirs(System.getProperties().getProperty("user.home")+File.separator+"Desktop"+File.separator+"Dir", 10); 
    } 
}