Create a File in a Specific Directory in Java – 在Java中创建一个特定目录下的文件

最后修改: 2019年 9月 15日

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

1. Overview

1.概述

In this quick tutorial, we’re going to look at how to create a file in a specific directory.

在这个快速教程中,我们将看看如何在特定目录下创建一个文件

We’ll see the difference between absolute and relative file paths, and we’ll use paths that work on several major operating systems.

我们将看到绝对文件路径和相对文件路径之间的区别,我们将使用在几个主要操作系统上工作的路径。

2. Absolute and Relative File Paths

2.绝对文件路径和相对文件路径

2.1. Absolute Paths

2.1.绝对路径

Let’s start with creating a file in a directory by referring to the entire path, also known as an absolute path. To demonstrate, we’ll use the absolute path to the user temp directory, and add our file into it.

让我们首先通过引用整个路径(也称为绝对路径)在一个目录中创建一个文件。为了演示,我们将使用用户temp目录的绝对路径,并将我们的文件加入其中。

We’re using Files.touch(), which is part of Google Guava, as an easy way to create an empty file:

我们使用Files.touch(),它是Google Guava的一部分,作为创建一个空文件的简单方法。

File tempDirectory = new File(System.getProperty("java.io.tmpdir"));
File fileWithAbsolutePath = new File(tempDirectory.getAbsolutePath() + "/testFile.txt");

assertFalse(fileWithAbsolutePath.exists());

Files.touch(fileWithAbsolutePath);

assertTrue(fileWithAbsolutePath.exists());

2.2. Relative Paths

2.2.相对路径

We can also create a file in a directory that is relative to another directory. For instance, let’s create file in the user temp directory:

我们也可以在一个相对于另一个目录的目录中创建一个文件。例如,让我们在用户temp目录下创建文件。

File tempDirectory = new File(System.getProperty("java.io.tmpdir"));
File fileWithRelativePath = new File(tempDirectory, "newFile.txt");

assertFalse(fileWithRelativePath.exists());

Files.touch(fileWithRelativePath);

assertTrue(fileWithRelativePath.exists());

In the above example, our new file is added to the path of the user temp directory.

在上述例子中,我们的新文件被添加到用户temp目录的路径中。

3. Using a Platform Independent File Separator

3.使用独立于平台的文件分离器

To construct file paths, we need to use separators like / or \. However, the appropriate separator to use depends upon your operating system. Luckily, there’s an easier way. We can use Java’s File.separator instead of separator characters. As a result, Java picks the appropriate separator for us.

为了构建文件路径,我们需要使用像//这样的分隔符。然而,使用适当的分隔符取决于你的操作系统。幸运的是,有一个更简单的方法。我们可以使用Java的File.separator来代替分隔符。因此,Java会为我们挑选合适的分隔符。

Let’s look at an example of creating a file with this method:

让我们看看用这种方法创建文件的例子。

File tempDirectory = new File(System.getProperty("java.io.tmpdir"));
File newFile = new File(tempDirectory.getAbsolutePath() + File.separator + "newFile.txt");

assertFalse(newFile.exists());

Files.touch(newFile);

assertTrue(newFile.exists());

Using File.separator, Java knows how to construct paths based on the underlying filesystem.

使用File.separator,Java知道如何根据底层文件系统来构建路径。

4. Conclusion

4.结论

In this article, we explored the differences between absolute and relative paths and how to create file paths that work on several major operating systems.

在这篇文章中,我们探讨了绝对路径和相对路径的区别,以及如何创建能在几个主要操作系统上工作的文件路径。

As always, the example code is available over on GitHub.

像往常一样,示例代码可在GitHub上获得