Java File Separator vs File Path Separator – Java文件分离器与文件路径分离器的比较

最后修改: 2021年 1月 7日

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

1. Overview

1.概述

Different operating systems use different characters as file and path separators. When our application has to run on multiple platforms, we need to handle these correctly.

不同的操作系统使用不同的字符作为文件和路径分隔符。当我们的应用程序必须在多个平台上运行时,我们需要正确处理这些问题。

Java helps us to pick an appropriate separator and provides functions to help us create paths that work on the host’s operating system.

Java帮助我们选择一个合适的分离器,并提供函数帮助我们创建在主机操作系统上工作的路径。

In this short tutorial, we’ll understand how to write code to use the correct file and path separators.

在这个简短的教程中,我们将了解如何编写代码以使用正确的文件和路径分隔符。

2. File Separator

2.文件分离器

The file separator is the character used to separate the directory names that make up the path to a specific location.

文件分隔符是用来分隔构成特定位置路径的目录名的字符。

2.1. Get the File Separator

2.1.获取文件分隔符

There are several ways to get the file separator in Java.

在Java中,有几种方法可以获得文件分隔符。

We can get the separator as a String using File.separator:

我们可以使用File.separator获得分隔符的String

String fileSeparator = File.separator;

We can also get this separator as a char with File.separatorChar:

我们也可以用File.separatorChar获得这个分隔符的字符

char fileSeparatorChar = File.separatorChar;

Since Java 7, we’ve also been able to use FileSystems:

从Java 7开始,我们也可以使用FileSystems

String fileSeparator = FileSystems.getDefault().getSeparator();

The output will depend on the host operating system. The file separator is \ on Windows and / on macOS and Unix-based operating systems.

输出结果将取决于主机操作系统。文件分隔符在Windows上是,在macOS和基于Unix的操作系统上是/

2.2. Construct a File Path

2.2.构建文件路径

Java provides a couple of methods for constructing a file path from its list of directories.

Java提供了一些方法来从其目录列表中构建文件路径。

Here, we’re using the Paths class:

这里,我们使用的是Paths类。

Path path = Paths.get("dir1", "dir2");

Let’s test it on Microsoft Windows:

让我们在Microsoft Windows上测试一下。

assertEquals("dir1\\dir2", path.toString());

Similarly, we can test it on Linux or Mac:

同样,我们也可以在Linux或Mac上测试它。

assertEquals("dir1/dir2", path.toString());

We can also use the File Class:

我们还可以使用文件类。

File file = new File("file1", "file2");

Let’s test it on Microsoft Windows:

让我们在Microsoft Windows上测试一下。

assertEquals("file1\\file2", file.toString());

Similarly, we can test it on Linux or Mac:

同样,我们也可以在Linux或Mac上测试它。

assertEquals("file1/file2", file.toString());

As we see, we can just provide path strings to construct a file path — we don’t need to include a file separator explicitly.

正如我们所看到的,我们可以只提供路径字符串来构建一个文件路径–我们不需要明确地包括一个文件分隔符。

3. Path Separator

3.路径分离器

The path separator is a character commonly used by the operating system to separate individual paths in a list of paths.

路径分隔符是操作系统常用的一个字符,用于分隔路径列表中的各个路径。

3.1. Get the Path Separator

3.1.获取路径分离器

We can get the path separator as a String using File.pathSeparator:

我们可以使用File.pathSeparator获得路径分隔符的String

String pathSeparator = File.pathSeparator;

We can also get the path separator as a char:

我们也可以将路径分隔符作为char来获取。

char pathSeparatorChar = File.pathSeparatorChar;

Both examples return the path separator. It is a semicolon (;) on Windows and colon (:) on Mac and Unix-based operating systems.

这两个例子都返回路径分隔符。在Windows上是分号(;,在Mac和基于Unix的操作系统上是冒号(:)。

3.2. Construct a File Path

3.2.构建文件路径

We can construct a file path as a String using the separator character as a delimiter.

我们可以用分隔符作为分隔符将文件路径构建为String

Let’s try the String.join method:

让我们试试String.join方法:

String[] pathNames = { "path1", "path2", "path3" };
String path = String.join(File.pathSeparator, pathNames);

Here we test our code on Windows:

这里我们在Windows上测试我们的代码。

assertEquals("path1;path2;path3", path);

And file path will look different on Linux or Mac:

而文件路径在Linux或Mac上看起来会有所不同。

assertEquals("path1:path2:path3", path);

Similarly, we can use the StringJoiner class:

类似地,我们可以使用StringJoiner类:

public static StringJoiner buildPathUsingStringJoiner(String path1, String path2) {
    StringJoiner joiner = new StringJoiner(File.pathSeparator);
    joiner.add(path1);
    joiner.add(path2);
    return joiner;
}

Let’s test our code on Microsoft Windows:

让我们在Microsoft Windows上测试我们的代码。

assertEquals("path1;path2", buildPathUsingStringJoiner("path1", "path2"));

And it will behave differently on Mac or Unix:

而且它在Mac或Unix上的表现会有所不同。

assertEquals("path1:path2", buildPathUsingStringJoiner("path1", "path2"));

4. Conclusion

4.结论

In this short article, we learned how to construct paths using system-specific file separators so our code can work on multiple operating systems.

在这篇短文中,我们学习了如何使用系统特定的文件分隔符构建路径,以便我们的代码能够在多个操作系统上工作。

We saw how to use the built-in classes Path and File to construct file paths, and we saw how to get the necessary separator to use with String concatenation utilities.

我们看到了如何使用内置类PathFile来构建文件路径,我们还看到了如何获得必要的分隔符以用于String连接工具。

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

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