java 如何在Java中创建文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/3024002/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a folder in Java?
提问by user364669
How can I create an empty folder in Java?
如何在 Java 中创建一个空文件夹?
回答by Luc M
File f = new File("C:\TEST");
try{
    if(f.mkdir()) { 
        System.out.println("Directory Created");
    } else {
        System.out.println("Directory is not created");
    }
} catch(Exception e){
    e.printStackTrace();
} 
回答by SLaks
回答by micha
With Java 7 and newer you can use the static Files.createDirectory()method of the java.nio.file.Filesclass along with Paths.get.
在 Java 7 和更新版本中,您可以将类的静态Files.createDirectory()方法java.nio.file.Files与Paths.get.
Files.createDirectory(Paths.get("/path/to/folder"));
The method Files.createDirectories()also creates parent directories if these do not exist.
如果父目录不存在,则Files.createDirectories()方法也会创建父目录。
回答by Andy White
Use the mkdir method on the File class:
在 File 类上使用 mkdir 方法:
http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html#mkdir%28%29
http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html#mkdir%28%29
回答by Wendel
Using Java 8:
使用 Java 8:
Files.createDirectories(Paths.get("/path/to/folder"));
Same:
相同的:
new File("/path/to/folder").mkdirs();
Or
或者
Files.createDirectory(Paths.get("/path/to/folder"));
Same:
相同的:
new File("/path/to/folder").mkdir();
回答by Ripon Al Wasim
The following code would be helpful for the creation of single or multiple directories:
以下代码将有助于创建单个或多个目录:
import java.io.File;
public class CreateSingleOrMultipleDirectory{
    public static void main(String[] args) {
//To create single directory
        File file = new File("D:\Test");
        if (!file.exists()) {
            if (file.mkdir()) {
                System.out.println("Folder/Directory is created successfully");
            } else {
                System.out.println("Directory/Folder creation failed!!!");
            }
        }
//To create multiple directories
        File files = new File("D:\Test1\Test2\Test3");
        if (!files.exists()) {
            if (files.mkdirs()) {
                System.out.println("Multiple directories are created successfully");
            } else {
                System.out.println("Failed to create multiple directories!!!");
            }
        }
    }
}
回答by Toukea Tatsi
Better to use mkdirsas: 
更好地mkdirs用作:
new File("dirPath/").mkdirs();
mkdirs: also create parent directories if these do not exist.  
mkdirs: 如果这些不存在,也创建父目录。  
ps: don't forget the ending /that shows explicitly you want to make a directory.
ps:不要忘记/明确显示您要创建目录的结尾。

