如何在java中创建目录?

nqwrtyyt  于 2021-07-06  发布在  Java
关注(0)|答案(17)|浏览(529)

如何创建目录/文件夹?
一旦我测试过 System.getProperty("user.home"); 当且仅当新文件夹不存在时,我必须创建一个目录(目录名“new folder”)。

p8h8hvxi

p8h8hvxi16#

您也可以参考makdir()函数在您想要的文件夹中创建一个新目录。

oyxsuwqo

oyxsuwqo17#

整洁干净:

import java.io.File;

public class RevCreateDirectory {

    public void revCreateDirectory() {
        //To create single directory/folder
        File file = new File("D:\\Directory1");
        if (!file.exists()) {
            if (file.mkdir()) {
                System.out.println("Directory is created!");
            } else {
                System.out.println("Failed to create directory!");
            }
        }
        //To create multiple directories/folders
        File files = new File("D:\\Directory2\\Sub2\\Sub-Sub2");
        if (!files.exists()) {
            if (files.mkdirs()) {
                System.out.println("Multiple directories are created!");
            } else {
                System.out.println("Failed to create multiple directories!");
            }
        }

    }
}

相关问题