如何创建目录/文件夹?

一旦我测试了System.getProperty("user.home");

我必须创建一个目录(目录名“新文件夹”)当且仅当新文件夹不存在时。


当前回答

虽然这个问题已经有了答案。我想多放点东西,比如。 如果存在与您试图创建的目录名称相同的文件,则会提示错误。为将来的访客准备的。

public static void makeDir()
{
    File directory = new File(" dirname ");
    if (directory.exists() && directory.isFile())
    {
        System.out.println("The dir with name could not be" +
        " created as it is a normal file");
    }
    else
    {
        try
        {
            if (!directory.exists())
            {
                directory.mkdir();
            }
            String username = System.getProperty("user.name");
            String filename = " path/" + username + ".txt"; //extension if you need one

        }
        catch (IOException e)
        {
            System.out.println("prompt for error");
        }
    }
}

其他回答

public class Test1 {
    public static void main(String[] args)
    {
       String path = System.getProperty("user.home");
       File dir=new File(path+"/new folder");
       if(dir.exists()){
           System.out.println("A folder with name 'new folder' is already exist in the path "+path);
       }else{
           dir.mkdir();
       }

    }
}

整洁干净:

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

    }
}

这是java的一个吸引力,使用短路或'||',测试目录的存在,并为您制作目录

public File checkAndMakeTheDirectory() {
    File theDirectory = new File("/path/directory");
    if (theDirectory.exists() || theDirectory.mkdirs())
        System.out.println("The folder has been created or has been already there");
    return theDirectory;
}

如果If的第一部分为真,它不会运行第二部分,如果第一部分为假,它也会运行第二部分

虽然这个问题已经有了答案。我想多放点东西,比如。 如果存在与您试图创建的目录名称相同的文件,则会提示错误。为将来的访客准备的。

public static void makeDir()
{
    File directory = new File(" dirname ");
    if (directory.exists() && directory.isFile())
    {
        System.out.println("The dir with name could not be" +
        " created as it is a normal file");
    }
    else
    {
        try
        {
            if (!directory.exists())
            {
                directory.mkdir();
            }
            String username = System.getProperty("user.name");
            String filename = " path/" + username + ".txt"; //extension if you need one

        }
        catch (IOException e)
        {
            System.out.println("prompt for error");
        }
    }
}

大约7年后,我会将其更新为Bozho建议的更好的方法。

File theDir = new File("/path/directory");
if (!theDir.exists()){
    theDir.mkdirs();
}