Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4535

Create a Directory in Java

$
0
0

1. Overview

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:

assertTrue(newDirectory.exists());
assertFalse(newDirectory.mkdir());

Then, as we expected, the method returns false on the second call.

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 false when all the directories already exist. If at least one directory is created the method will return true.

Therefore this means that the mkdirs() method used on a directory whose parents exist will work the same as the mkdir() method.

4. Conclusion

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.


Viewing all articles
Browse latest Browse all 4535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>