Create a Directory in Java – 在Java中创建一个目录

最后修改: 2019年 1月 20日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

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().

用Java创建一个目录是非常简单的。该语言为我们提供了两种方法,允许我们创建一个单一的目录或多个嵌套的目录 – mkdir()mkdirs()

In this tutorial, we’ll see how they both behave.

在本教程中,我们将看到它们如何表现。

2. Create a Single Directory

2.创建一个单一的目录

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”).

为了我们的目的,我们将利用用户的temp目录。我们可以用System.getProperty(“java.io.tmpdir”)来查找它。

We’ll pass this path to a Java File object, which will represent our temp directory:

我们将把这个路径传递给一个Java File 对象,它将代表我们的临时目录。

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:

现在让我们在其中创建一个新的目录。我们将通过在一个代表要创建的目录的新文件对象上调用文件::mkdir方法来实现:

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.

为了确保我们的目录还不存在,我们首先使用exists()方法。

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.

然后我们调用mkdir()方法,告诉我们目录创建是否成功。如果该目录已经存在,该方法将返回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.

然后,如我们所料,该方法在第二次调用时返回false

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.

而且,mkdir()方法不仅在目录已经存在时返回false 而且在其他一些情况下。例如,可能存在一个与我们想要创建的目录名称相同的文件。或者我们可能缺乏创建这个目录的权限。

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:

考虑到这一点,我们必须找到一种方法来确保我们的目录最终存在,要么是我们创建了它,要么是它本来就存在。为此,我们可以使用isDirectory()方法>。

newDirectory.mkdir() || newDirectory.isDirectory()

That way, we make sure that the directory we need is there.

这样,我们就能确保我们需要的目录在那里。

3. Create Multiple Nested Directories

3.创建多个嵌套目录

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::mkdir对此不起作用。

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.

由于new_directory不存在,mkdir不会创建基础的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.

然而,文件类为我们提供了另一种方法来实现这一目标–mkdirs()这个方法的行为与mkdir()类似,但也将创建所有不存在的父目录。

In our previous example, this would mean creating not only nested_directory, but also new_directory.

在我们之前的例子中,这意味着不仅要创建nested_directory,还要创建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(File, String)构造函数,但是我们也可以使用File(String)构造函数,并使用File.separator传递我们文件的完整路径来分隔路径的不同部分。

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.

正如我们所看到的,这些目录是按预期创建的。此外,该方法只在至少一个目录被创建时返回true。至于mkdir()方法,在其他情况下它将返回false

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

因此,这意味着在一个父类存在的目录上使用的mkdirs()方法将与mkdir()方法的作用相同。

4. Conclusion

4.总结

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.

在这篇文章中,我们已经看到了两种允许我们在Java中创建目录的方法。第一个方法,mkdir(),目标是创建一个单一的目录,前提是它的父目录已经存在。第二个方法,mkdirs(),能够创建一个目录以及它不存在的父目录。

The code of this article can be found on our GitHub.

这篇文章的代码可以在我们的GitHub上找到