Java Create a New Directory using Files.createDirectory()

Tags: Files.createDirectory Java NIO

In this Java NIO tutorial we learn how to use the Files.createDirectory() method in Java application to create a new directory.

Table of contents

  1. How to use Files.createDirectory() method
  2. More Files.createDirectory() Java Examples

How to use Files.createDirectory() method

For example, we have a directory D:\SimpleSolution and want to create a new sub directory named Data inside it, we can use the Files.createDirectory() method as following Java program.

FilesCreateDirectoryExample1.java

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

public class FilesCreateDirectoryExample1 {
    public static void main(String... args) throws IOException {
        Path directoryToCreate = Paths.get("D:\\SimpleSolution\\Data");

        Path createdDirectory = Files.createDirectory(directoryToCreate);

        System.out.println("Successful create directory " + createdDirectory);
    }
}
The output as below.
Successful create directory D:\SimpleSolution\Data

If there is the directory D:\SimpleSolution\Data already existed the application will throws java.nio.file.FileAlreadyExistsException exception as below.

Exception in thread "main" java.nio.file.FileAlreadyExistsException: D:\SimpleSolution\Data
	at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:81)
	at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
	at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
	at sun.nio.fs.WindowsFileSystemProvider.createDirectory(WindowsFileSystemProvider.java:504)
	at java.nio.file.Files.createDirectory(Files.java:674)
	at FilesCreateDirectoryExample1.main(FilesCreateDirectoryExample1.java:10)

To avoid the above exception, remember to check the directory existing before create the new one using the Files.exists() method. Below is the modified Java application.

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

public class FilesCreateDirectoryExample1 {
    public static void main(String... args) throws IOException {
        Path directoryToCreate = Paths.get("D:\\SimpleSolution\\Data");

        if( !Files.exists(directoryToCreate)) {
            Path createdDirectory = Files.createDirectory(directoryToCreate);

            System.out.println("Successful create directory " + createdDirectory);
        }
    }
}

In case you want to create the directory within multiple sub directories for example D:\SimpleSolution\Data\Java\JavaIO but there is no sub directory named Java inside D:\SimpleSolution\Data then the application will throw java.nio.file.NoSuchFileException exception as below Java program.

FilesCreateDirectoryExample2.java

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

public class FilesCreateDirectoryExample2 {
    public static void main(String... args) throws IOException {
        Path directoryToCreate = Paths.get("D:\\SimpleSolution\\Data\\Java\\JavaIO");

        Path createdDirectory = Files.createDirectory(directoryToCreate);

        System.out.println("Successful create directory " + createdDirectory);
    }
}
The output as below.
Exception in thread "main" java.nio.file.NoSuchFileException: D:\SimpleSolution\Data\Java\JavaIO
	at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
	at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
	at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
	at sun.nio.fs.WindowsFileSystemProvider.createDirectory(WindowsFileSystemProvider.java:504)
	at java.nio.file.Files.createDirectory(Files.java:674)
	at FilesCreateDirectoryExample2.main(FilesCreateDirectoryExample2.java:10)

To avoid above exception, just make sure the directory D:\SimpleSolution\Data\Java\ existed before you call Files.createDirectory() method. Or you can use Files.createDirectories() method to create parent and sub directories.

More Files.createDirectory() Java Examples

In the following Java program, we use the Files.createDirectory() method to create a new temporary directory inside user’s temporary directory.

FilesCreateDirectoryExample3.java

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

public class FilesCreateDirectoryExample3 {
    public static void main(String... args) throws IOException {
        Path temporaryDirectory = Paths.get(System.getProperty("java.io.tmpdir"));
        Path directoryToCreate = temporaryDirectory.resolve("SimpleSolution");

        Path createdDirectory = Files.createDirectory(directoryToCreate);

        System.out.println("Successful create directory " + createdDirectory);
    }
}
The output as below.
Successful create directory C:\Users\SS\AppData\Local\Temp\SimpleSolution

Happy Coding 😊

Java Create Parent and Sub Directories using Files.createDirectories()

Java Copy File using Files.copy()

Java Get All Entries in Directory using Files.newDirectoryStream()

Java Create New File using Files.createFile()