java - new - scala创建文件夹




如何在Java中创建文件夹? (6)

如何在Java中创建一个空文件夹?


以下代码有助于创建单个或多个目录:

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!!!");
            }
        }
    }
}

使用mkdir()

new File('/path/to/folder').mkdir();

使用Java 8:

Files.createDirectories(Paths.get("/path/to/folder"));

相同:

new File("/path/to/folder").mkdirs();

要么

Files.createDirectory(Paths.get("/path/to/folder"));

相同:

new File("/path/to/folder").mkdir();


调用File.mkdir ,如下所示:

new File(path).mkdir();

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();
} 




createfolder