How to create a folder in Java?

JavaDirectoryCreate Directory

Java Problem Overview


How can I create an empty folder in Java?

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

Call File.mkdir, like this:

new File(path).mkdir();

Solution 3 - Java

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.

Solution 4 - Java

Use mkdir():

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

Solution 5 - Java

Solution 6 - Java

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

Solution 7 - Java

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.

Solution 8 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser364669View Question on Stackoverflow
Solution 1 - JavaLuc MView Answer on Stackoverflow
Solution 2 - JavaSLaksView Answer on Stackoverflow
Solution 3 - JavamichaView Answer on Stackoverflow
Solution 4 - JavaMattView Answer on Stackoverflow
Solution 5 - JavaAndy WhiteView Answer on Stackoverflow
Solution 6 - JavaWendelView Answer on Stackoverflow
Solution 7 - JavaToukea TatsiView Answer on Stackoverflow
Solution 8 - JavaRipon Al WasimView Answer on Stackoverflow