How to create directory in Java

How to create directory in Java

In this post, we will see how to create directory in Java.

We will be covering following options to understand how to create directory in Java

  1. Create a directory using file I/O introduced in Java 7 (java.nio package).
  2.  Apache Commons IO

 

1. Using Java 7

Java 7 Introduced java.nio.file system which provided 2 methods for creating a directory in Java.

public static Path createDirectory(Path dir, FileAttribute<?>... attrs)

public static Path createDirectories(Path dir,FileAttribute<?>... attrs)


createDirectory method is used to create a directory but will throw an exception in case parent directory does not exist into the system. Use this method only when you parent directory already exists in the file system.

createDirectories method is useful when we want to create all nonexistence nonexistent parent directories first.

1.1 Use CreteDirectory Method

 

public class CreateDirectory {

    public static void main(String[] args) throws IOException {

        Path path = Paths.get("/Users/personal/tutorials/directory");
        Path newPath= Files.createDirectory(path);
    }
}

Above code will create new directory with name "directory" under tutorials.

Above code will throw NoSuchFileException in case hierarchy does not exist.If we will change hierarchy to /Users/personal/tutorials1/directory (note tutorials1 does not exist in the underlying file system)

Output


Exception in thread "main" java.nio.file.NoSuchFileException: /Users/personal/tutorials1/directory
	at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
	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 com.umeshawasthi.tutorials.corejava.io.directory.CreateDirectory.main(CreateDirectory.java:16)

It will throw FileAlreadyExistsException in the case of directory structure already exists.Note that for createDirectory, check for the existence of the file and the creation of the directory if it does not exist are a single operation

1.2 Use CreteDirectories Method

For creating all nonexistent parent directories first, createDirectories method should be used

 


public class CreateDirectories {

    public static void main(String[] args) throws IOException {
        Path path = Paths.get("/Users/umesh/personal/tutorials_new/newDirectory");
        Files.createDirectories(path);
    }
} 

The method will not throw an exception if the directory could not be created because it already exists.In case it fails, it may do so after creating some, but not all, of the parent directories.

Both these methods will delegate work to FileSystemProvider.

FileAttribute attrs... is an optional parameter which can be used to set file attributes while creating file /directory.

For most of the case, FileAttribute will be set automatically.

 

1. Using Apache Commons IO

Apache Commons FileUtils#forceMkdir provide a convenient and easy way to create a directory in Java.


public class CreateDirectoryByFileUtils {

    public static void main(String[] args) throws IOException {
        FileUtils.forceMkdir(new java.io.File("/Users/umesh/personal/tutorials/directory"));
    }
}

FileUtils class provides two methods to create directory structure

public static void forceMkdir(File directory)
public static void forceMkdirParent(File file)

In this post, we checked different options how to create a directory in Java. Java 7 NIO package got a good enhancement to the file I/O package.

All the code of this article is available Over on Github. This is a Maven-based project.

References

  1. CreateDirectory
  2. CreateDirectories
  3. FileUtils.html#forceMkdir
  4. FileUtils#forceMkdirParent

Comments are closed.