How to create a folder in Java?
Asked Answered
V

8

58

How can I create an empty folder in Java?

Valise answered 11/6, 2010 at 15:17 Comment(2)
Hmm.. I Googled and this was the first result (;Coadjutrix
@CoolBeans The creators of StackOverflow have said they want questions here to be the first hit on Google. Nothing wrong with creating a simple google-able question here if it is clearly stated and original (not a duplicate on StackOverflow).Grating
P
77
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();
} 
Pavlov answered 11/6, 2010 at 15:23 Comment(5)
Just wondering: What case would trigger the else-branch? Wouldn't there be an exception whenever the creation fails?Pantheon
Isn't it mkdir (all lower case)? @0xA3: mkdir throws SecurityExceptions, but surely that won't happen if C:\TEST doesn't exist?Questionless
@OxA3 If you don't have right to create the directory, the else branch is executed.Pavlov
There's also mkdirs which will create parent folders too: docs.oracle.com/javase/7/docs/api/java/io/File.html#mkdirs()Hagiarchy
Please don't use catch(Exception e){e.printStackTrace()}: today.java.net/article/2006/04/04/…Allen
P
21

Call File.mkdir, like this:

new File(path).mkdir();
Pennsylvanian answered 11/6, 2010 at 15:19 Comment(0)
I
20

With Java 7 and newer you can use the static Files.createDirectory() method of the java.nio.file.Files class along with Paths.get.

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

The method Files.createDirectories() also creates parent directories if these do not exist.

Indistinguishable answered 21/5, 2014 at 17:51 Comment(2)
Files.createDirectories() will also silently ignore already existing directories.Isosceles
@Isosceles This is only valid for parent directories. If the main directory (in this case "folder") already exists, it will thrown an exception (see also javadoc).Cyanamide
S
6

Use mkdir():

new File('/path/to/folder').mkdir();
Synchronous answered 11/6, 2010 at 15:19 Comment(0)
S
5

Use the mkdir method on the File class:

https://docs.oracle.com/javase/1.5.0/docs/api/java/io/File.html#mkdir%28%29

Stamp answered 11/6, 2010 at 15:19 Comment(0)
S
4

Using 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();
Stylite answered 1/6, 2016 at 14:52 Comment(0)
B
0

Better to use mkdirs as:

new File("dirPath/").mkdirs();

mkdirs: also create parent directories if these do not exist.

ps: don't forget the ending / that shows explicitly you want to make a directory.

Bruni answered 8/6, 2015 at 15:7 Comment(0)
L
0

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!!!");
            }
        }
    }
}
Littlest answered 3/5, 2018 at 12:19 Comment(1)
You can follow the link also: roseindia.net/java/beginners/java-create-directory.shtmlLittlest

© 2022 - 2024 — McMap. All rights reserved.