58

How can I create an empty folder in Java?

tshepang
  • 11,360
  • 21
  • 88
  • 132
user364669
  • 591
  • 1
  • 4
  • 4
  • 35
    Hmm.. I Googled and this was the first result (; – dcow Jul 07 '12 at 19:49
  • 8
    @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

76
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
  • 190,799
  • 110
  • 376
  • 555
Luc M
  • 15,742
  • 26
  • 71
  • 88
  • 1
    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
  • 5
    @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
  • 837,282
  • 173
  • 1,862
  • 1,933
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
  • 262,936
  • 84
  • 758
  • 1,028
micha
  • 45,376
  • 16
  • 70
  • 78
  • `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). – user1451252 Feb 22 '21 at 15:40
6

Use mkdir():

new File('/path/to/folder').mkdir();
Matt
  • 42,312
  • 6
  • 93
  • 99
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
  • 539
  • 1
  • 5
  • 7
Andy White
  • 84,168
  • 47
  • 173
  • 207
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,489
  • 23
  • 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
  • 15,742
  • 26
  • 71
  • 88
Toukea Tatsi
  • 149
  • 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
  • 35,466
  • 40
  • 150
  • 172