59

How can I create an empty folder in Java?

tshepang
  • 12,111
  • 21
  • 91
  • 136
user364669
  • 601
  • 1
  • 5
  • 4
  • 35
    Hmm.. I Googled and this was the first result (; – dcow Jul 07 '12 at 19:49
  • 9
    @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). – Basil Bourque Jun 27 '14 at 22:06

8 Answers8

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();
} 
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
Luc M
  • 16,630
  • 26
  • 74
  • 89
  • 2
    Just wondering: What case would trigger the else-branch? Wouldn't there be an exception whenever the creation fails? – Dirk Vollmar Jun 11 '10 at 15:29
  • 1
    Isn't it `mkdir` (all lower case)? @0xA3: mkdir throws SecurityExceptions, but surely that won't happen if C:\TEST doesn't exist? – Peter Jaric Jun 11 '10 at 16:12
  • 6
    @OxA3 If you don't have right to create the directory, the else branch is executed. – Luc M Jun 11 '10 at 16:23
  • 3
    There's also `mkdirs` which will create parent folders too: http://docs.oracle.com/javase/7/docs/api/java/io/File.html#mkdirs() – CC. Mar 25 '14 at 22:53
  • Please don't use `catch(Exception e){e.printStackTrace()}`: https://today.java.net/article/2006/04/04/exception-handling-antipatterns#catchingException – Nikita Bosik Nov 19 '14 at 14:55
21

Call File.mkdir, like this:

new File(path).mkdir();
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
19

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
micha
  • 47,774
  • 16
  • 73
  • 80
  • `Files.createDirectories()` will also silently ignore already existing directories. – gronostaj Oct 24 '17 at 07:55
  • @gronostaj 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). – Marius Feb 22 '21 at 15:40
6

Use mkdir():

new File('/path/to/folder').mkdir();
Matt
  • 43,482
  • 6
  • 101
  • 102
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

fospathi
  • 537
  • 1
  • 6
  • 7
Andy White
  • 86,444
  • 48
  • 176
  • 211
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();
Wendel
  • 2,809
  • 29
  • 28
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.

Luc M
  • 16,630
  • 26
  • 74
  • 89
Toukea Tatsi
  • 189
  • 1
  • 5
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!!!");
            }
        }
    }
}
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176