Creating a directory with Java is pretty straight-forward. The language provides us with two methods allowing us to create either a single directory or multiple nested directories – mkdir() and mkdirs().
In this tutorial, we’ll see how they both behave.
2. Create a Single Directory
Let’s start with the creation of a single directory.
For our purposes, we’ll make use of the user temp directory. We can look it up with System.getProperty(“java.io.tmpdir”).
We’ll pass this path to a Java File object, which will represent our temp directory:
private static final File TEMP_DIRECTORY = new File(System.getProperty("java.io.tmpdir"));
Now let’s create a new directory inside of it. We’ll achieve this by calling the File::mkdir method on a new File object representing the directory to create:
File newDirectory = new File(TEMP_DIRECTORY, "new_directory"); assertFalse(newDirectory.exists()); assertTrue(newDirectory.mkdir());
To ensure our directory doesn’t exist yet, we first used the exists() method.
Then we called the mkdir() method that tells us if the directory creation succeeded or not. If the directory already existed, the method would have returned false.
If we make the same calls again:
Then, as we expected, the method returns false on the second call.
And, the mkdir() method not only return false when the directory already exists but also in some other situations. For example, a file could exist with the name of the directory we want to create. Or we could lack the permissions to create this directory.
With that in mind, we have to find a way to make sure our directory exists in the end, either we created it or it was already there. For that purpose, we could use the isDirectory() method:
newDirectory.mkdir() || newDirectory.isDirectory()
That way, we make sure that the directory we need is there.
3. Create Multiple Nested Directories
What we’ve seen so far works well on a single directory, but what happens if we want to create multiple nested directories?
In the following example, we’ll see that File::mkdir doesn’t work for that:
File newDirectory = new File(TEMP_DIRECTORY, "new_directory"); File nestedDirectory = new File(newDirectory, "nested_directory"); assertFalse(newDirectory.exists()); assertFalse(nestedDirectory.exists()); assertFalse(nestedDirectory.mkdir());
As the new_directory doesn’t exist mkdir doesn’t create the underlying nested_directory.
However, the File class provides us with another method to achieve that – mkdirs(). This method will behave like mkdir() but will also create all the unexisting parent directories as well.
In our previous example, this would mean creating not only nested_directory, but also new_directory.
Note that until now we used the File(File, String) constructor, but we can also use the File(String) constructor and pass the complete path of our file using File.separator to separate the different parts of the path:
File newDirectory = new File(System.getProperty("java.io.tmpdir") + File.separator + "new_directory"); File nestedDirectory = new File(newDirectory, "nested_directory"); assertFalse(newDirectory.exists()); assertFalse(nestedDirectory.exists()); assertTrue(nestedDirectories.mkdirs());
As we can see, the directories are created as expected. Moreover, the method only returns true when at least one directory is created. As for the mkdir() method, it’ll return false in the other cases.
Therefore this means that the mkdirs() method used on a directory whose parents exist will work the same as the mkdir() method.
In this article, we’ve seen two methods allowing us to create directories in Java. The first one, mkdir(), targets the creation of a single directory, provided its parents already exist. The second one, mkdirs(), is able to create a directory as well as its unexisting parents.
The code of this article can be found on our GitHub.