How to create directory in Java

To create a directory in Java, uses the following code:

1. Standard Java IO package – java.io.File

1.1 Create a single directory.


new File("C:\\Directory1").mkdir();

1.2 Create a directory named “Directory2 and all its sub-directories “Sub2” and “Sub-Sub2” together.


new File("C:\\Directory2\\Sub2\\Sub-Sub2").mkdirs()

P.S Both method mkdir() and mkdirs() are returning a boolean value to indicate the operation status : true if succeed, false otherwise.

2. For JDK 7, try Java NIO package – java.nio.file.Paths and java.nio.file.Files


Path path = Paths.get("C:\\Directory1");
Files.createDirectories(path);

1. Java IO Example

A classic Java IO directory example, check if directory exists, if no, then create it.

CreateDirectoryExample.java

package com.mkyong.file;

import java.io.File;

public class CreateDirectoryExample {

    public static void main(String[] args) {

        File file = new File("C:\\Directory1");
        if (!file.exists()) {
            if (file.mkdir()) {
                System.out.println("Directory is created!");
            } else {
                System.out.println("Failed to create directory!");
            }
        }

        File files = new File("C:\\Directory2\\Sub2\\Sub-Sub2");
        if (!files.exists()) {
            if (files.mkdirs()) {
                System.out.println("Multiple directories are created!");
            } else {
                System.out.println("Failed to create multiple directories!");
            }
        }

    }

}

2. Java NIO Example

Java NIO classes are added in JDK 7.

CreateDirectoryExample.java

package com.mkyong.file;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class CreateDirectoryExample {
    public static void main(String[] args) {

        Path path = Paths.get("C:\\Directory2\\Sub2\\Sub-Sub2");
        //if directory exists?
        if (!Files.exists(path)) {
            try {
                Files.createDirectories(path);
            } catch (IOException e) {
                //fail to create directory
                e.printStackTrace();
            }
        }

    }
    
}

If a directory is failing to create, IOException will be thrown, for example

java.nio.file.AccessDeniedException: /directory-name
	at sun.nio.fs.UnixException.translateToIOException(UnixException.java:84)
	at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
	at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
	at sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:384)
	at java.nio.file.Files.createDirectory(Files.java:674)
	at java.nio.file.Files.createAndCheckIsDirectory(Files.java:781)
	at java.nio.file.Files.createDirectories(Files.java:767)

References

  1. java.nio.file.Files JavaDoc
  2. java.io.File JavaDoc
  3. Java NIO tutorials

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Debasish Halder
Guest
Debasish Halder

i want create folder on server like(http://localhost:8084/Test). how to create?
i use this type
File inFile = new File(“http://localhost:8084/Test/web/images”);
boolean b = inFile.mkdir();

its identify that : http:localhost:8084Testwebimeges

Shilpater
Guest
Shilpater

Just a correction:

File files = new File(“C:\Directory2\Sub2\Sub-Sub2”);
if (files.exists()) { <- should be !files.exists()
if (files.mkdirs()) {
System.out.println("Multiple directories are created!");
} else {
System.out.println("Failed to create multiple directories!");
}
}

Binh Thanh Nguyen
Guest
Binh Thanh Nguyen

Thanks, nice post

sawmon
Guest
sawmon

Hi mr.mkyong.I like your site.Your posts are useful for my java knowledge……

Aforallie
Guest
Aforallie

Your examples are great and very helpful. Thank you!

Hafifi
Guest
Hafifi

It is just basic example to get you started.
By learning the example you know what library that you need to use.

From there i assume that you will be exploring the library on you own and read the API.

akshay
Guest
akshay

Very easy program to create folder at specified path. Thanks

Dariusz
Guest
Dariusz

hi
you are missing an exclamation mark in the multiple directories
it should be

File files = new File("C:\\Directory2\\Sub2\\Sub-Sub2");
	if (<b>!</b>files.exists()) {
		if (files.mkdirs()) {

Could you delete other two posts thanks ;)

Dariusz
Guest
Dariusz

hi
you are missing an exclamation mark in the multiple directories
it should be

File files = new File(“C:\\Directory2\\Sub2\\Sub-Sub2”);
if (!files.exists()) {
if (files.mkdirs()) {

Dar
Guest
Dar

hi
you are missing an exclamation mark in the multiple directories
it should be

File files = new File("C:\\Directory2\\Sub2\\Sub-Sub2");
	if (<bold>!</bold>files.exists()) {
		if (files.mkdirs()) {
Mezbah uddin
Guest
Mezbah uddin

Really awesome site for java learner but filling the lackings of the tutorial for javax.swing.Its request to sir consider our demand.

aboulfazl
Guest
aboulfazl

thanks very much

Shruthi
Guest
Shruthi

Very Useful. Thank you very much

Madhumitha
Guest
Madhumitha

All of your posts are of great help. Thanks. Continue writing more tech posts :)

pravin
Guest
pravin

i like it but give some video to how to create directory

anupam ghos
Guest
anupam ghos

Hi Mkyoung,
Thank you for your posts. These are really helpful.
Regards
Anupam

trackback
Java I/O Tutorial

[…] Create directory […]

Patrick
Guest
Patrick

How can you create a file on your desktop? (I am using Windows and i am from Germany)