How to create a directory in Java?

2019-01-02 16:36发布

How do I create Directory/folder?

Once I have tested System.getProperty("user.home");

I have to create a directory (directory name "new folder" ) if and only if new folder does not exist.

17条回答
栀子花@的思念
2楼-- · 2019-01-02 17:04

Though this question has been answered. I would like to put something extra, i.e. if there is a file exist with the directory name that you are trying to create than it should prompt an error. For future visitors.

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");
        }
    }
}
查看更多
不再属于我。
3楼-- · 2019-01-02 17:04

Well to create Directory/folder in java we have two methods

Here makedirectory method creates single directory if it does not exist.

File dir = new File("path name");
boolean isCreated = dir.mkdir();

And

File dir = new File("path name");
boolean isCreated = dir.mkdirs();

Here makedirectories method will create all directories that are missing in the path which the file object represent.

For example refer link below (explained very well). Hope it helps!! https://www.flowerbrackets.com/create-directory-java-program/

查看更多
何处买醉
4楼-- · 2019-01-02 17:04

You can also refer makdir() function for creating a new directory in a folder where you want.

查看更多
有味是清欢
5楼-- · 2019-01-02 17:15

After ~7 year, I will update it to better approach which is suggested by Bozho.

new File("/path/directory").mkdirs();

Deprecated:

File theDir = new File("new folder");

// if the directory does not exist, create it
if (!theDir.exists()) {
    System.out.println("creating directory: " + theDir.getName());
    boolean result = false;

    try{
        theDir.mkdir();
        result = true;
    } 
    catch(SecurityException se){
        //handle it
    }        
    if(result) {    
        System.out.println("DIR created");  
    }
}
查看更多
人间绝色
6楼-- · 2019-01-02 17:18

Just wanted to point out to everyone calling File.mkdir() or File.mkdirs() to be careful the File object is a directory and not a file. For example if you call mkdirs() for the path /dir1/dir2/file.txt, it will create a folder with the name file.txt which is probably not what you wanted. If you are creating a new file and also want to automatically create parent folders you can do something like this:

            File file = new File(filePath);
            if (file.getParentFile() != null) {
                file.getParentFile().mkdirs();
            }
查看更多
倾城一夜雪
7楼-- · 2019-01-02 17:19
new File("/path/directory").mkdirs();

Here "directory" is the name of the directory you want to create/exist.

查看更多
登录 后发表回答