1. Overview 概述

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 文件路径分隔符

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 获取系统的文件路径分隔符

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

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

在Java语言中有几种方式来获取文件路径分隔符,如,可以使用File.separator来获取String型的分隔符,示例如下:

String fileSeparator = File.separator;

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

或者呢,使用File.separatorChar来获取 char型的分隔符,示例如下:

char fileSeparatorChar = File.separatorChar;

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

而从Java7开始,可以使用FileSystems类来获取FileSystem对象,再获取默认分隔符,示例如下:

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 构建一个文件路径

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

Here, we're using the Paths class:

Java语言提供了几种方法来构建一个有多个目录的文件路径,这里先来使用Paths类,示例如下:

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

Let's test it on Microsoft Windows: 在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 file = new File("file1", "file2");

Let's test it on Microsoft Windows: 在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 路径间的分隔符

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

路径分隔符是操作系统来分隔一组路径中的第个单独路径所使用的分隔符。

对此深有体会的是,在Mac上使用 javac命令编译java文件 及 java 命令行运行class文件时,使用 -cp -classpath -sourcepath 等参数指定多个路径时,一开始使用 分号 ; ,结果怎么都编译不成功,后来突然想到是Mac,应该使用 冒号 :,这都成功编译,运行时也是。

3.1. Get the Path Separator 获取系统默认的路径分隔符

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 构建一个路径字符串,包含多个独立路径

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: 在Mac和Linux上则会有些不同,如下:

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: 在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 最后

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.

我们看到了如何使用内置的 Path 和 File 类来构建文件路径,同样也熟悉了如何获取分隔符及使用 String 的连接功能。

大家点赞关注哈