1. Introduction

Creating a directory in Kotlin is as easy as it’s in Java. In this tutorial, we’ll look at different ways we can create either single directories or nested directory structures.

2. Creating a Single Directory

First, let’s take a look at the method that’s exposed by java.nio.file.Files to create ./createDirectory directory:


This simple method creates a directory if the provided path can be reached (more on this in a bit) and it doesn’t already exist.

If either of those conditions aren’t met, the createDirectory() method throws IOException:


Here, since createDirectory_2 doesn’t yet exist, the creation of inner fails. We’ll take a look at how to handle this issue in the next section.

We have one more method to create a single directory – File(…).mkdir():

val resultMkdir: Boolean = File("./mkdir").mkdir()

While Files.createDirectory() throws an exception, mkdir() instead returns a simple boolean. true result signifies successful file creation and false result signifies a failure to create a file.

There’s one thing to note here: with the lack of the exception that Files.createDirectory() provides, we can’t be sure of the exact reason mkdir() failed to create a directory. We need to be careful to pick the method that suits our needs best.

Now that we’ve looked at both options to create single directories, let’s see how we can create whole directory trees.

3. Creating Nested Directories

Let’s once again take a look at the method exposed by java.nio.file.Files to create ./createDirectories/inner directory structure:


As we can see, the usage is exactly the same as in the single directory scenario. The only differences are that it create all intermediary directories and it won’t fail if the directories already exist. In the above example, createDirectories is the intermediary directory, and inner is our target directory.

So while running the Files.createDirectory() for the same path twice would result in an exception, in the case of Files.createDirectories(), there’s no error:

println("Running Files.createDirectories 2nd time does nothing")

Similarly to how createDirectory() has a friend in the form of mkdir()createDirectories() has a corresponding method in the form of mkdirs():

val resultMkdirs: Boolean = File("./mkdirs/inner").mkdirs()

This method also returns a boolean, signifying either success or failure. Therefore, we once again cannot be sure as to the exact reason why the directory didn’t get created. If we need this information, then using Files.createDirectories() might be a better approach.

4. Conclusion

In this article, we looked at two methods to create either single or nested directories. Files.createDirectory() and Files.createDirectories() are a bit more restrictive but, in return, throw exceptions which give us a better picture of why we might have failed to create the directory.

On the other hand, we have mkdir() and mkdirs() methods, which return simple boolean instead of throwing an exception. We need to pick our method of choice according to our requirements.

As always, the code from this article is available over on GitHub.

Comments are closed on this article!