本文翻译自:File.separator or File.pathSeparator
In the File
class there are two strings, separator
and pathSeparator
. 在File
类中有两个字符串, separator
和pathSeparator
。
What's the difference? 有什么不同? When should I use one over the other? 我什么时候应该使用另一个?
#1楼
参考:https://stackoom.com/question/P3a0/File-separator或File-pathSeparator
#2楼
java.io.File
class contains four static separator variables. java.io.File
类包含四个静态分隔符变量。 For better understanding, Let's understand with the help of some code 为了更好地理解,让我们在一些代码的帮助下理解
- separator: Platform dependent default name-separator character as String. separator:平台相关的默认名称 - 分隔符字符串。 For windows, it's '\\' and for unix it's '/' 对于Windows,它是'\\'而对于unix它是'/'
- separatorChar: Same as separator but it's char separatorChar:与分隔符相同,但它是char
- pathSeparator: Platform dependent variable for path-separator. pathSeparator:路径分隔符的平台因变量。 For example PATH or CLASSPATH variable list of paths separated by ':' in Unix systems and ';' 例如,在Unix系统中用':'分隔的路径的PATH或CLASSPATH变量列表和';' in Windows system 在Windows系统中
- pathSeparatorChar: Same as pathSeparator but it's char pathSeparatorChar:与pathSeparator相同,但它是char
Note that all of these are final variables and system dependent. 请注意,所有这些都是最终变量和系统相关。
Here is the java program to print these separator variables. 这是打印这些分隔符变量的java程序。 FileSeparator.java FileSeparator.java
import java.io.File;
public class FileSeparator {
public static void main(String[] args) {
System.out.println("File.separator = "+File.separator);
System.out.println("File.separatorChar = "+File.separatorChar);
System.out.println("File.pathSeparator = "+File.pathSeparator);
System.out.println("File.pathSeparatorChar = "+File.pathSeparatorChar);
}
}
Output of above program on Unix system: 在Unix系统上输出以上程序:
File.separator = /
File.separatorChar = /
File.pathSeparator = :
File.pathSeparatorChar = :
Output of the program on Windows system: 在Windows系统上输出程序:
File.separator = \
File.separatorChar = \
File.pathSeparator = ;
File.pathSeparatorChar = ;
To make our program platform independent, we should always use these separators to create file path or read any system variables like PATH, CLASSPATH. 为了使程序平台独立,我们应该始终使用这些分隔符来创建文件路径或读取任何系统变量,如PATH,CLASSPATH。
Here is the code snippet showing how to use separators correctly. 以下是显示如何正确使用分隔符的代码段。
//no platform independence, good for Unix systems
File fileUnsafe = new File("tmp/abc.txt");
//platform independent and safe to use across Unix and Windows
File fileSafe = new File("tmp"+File.separator+"abc.txt");
#3楼
You use separator when you are building a file path. 在构建文件路径时使用分隔符。 So in unix the separator is /
. 所以在unix中,分隔符是/
。 So if you wanted to build the unix path /var/temp
you would do it like this: 所以如果你想构建unix路径/var/temp
你会这样做:
String path = File.separator + "var"+ File.separator + "temp"
You use the pathSeparator
when you are dealing with a list of files like in a classpath. 在处理类路径中的文件列表时,可以使用pathSeparator
。 For example, if your app took a list of jars as argument the standard way to format that list on unix is: /path/to/jar1.jar:/path/to/jar2.jar:/path/to/jar3.jar
例如,如果您的应用程序将jar列表作为参数,则在unix上格式化该列表的标准方法是: /path/to/jar1.jar:/path/to/jar2.jar:/path/to/jar3.jar
: /path/to/jar1.jar:/path/to/jar2.jar:/path/to/jar3.jar
: /path/to/jar1.jar:/path/to/jar2.jar:/path/to/jar3.jar
So given a list of files you would do something like this: 所以给定一个文件列表,你会做这样的事情:
String listOfFiles = ...
String[] filePaths = listOfFiles.split(File.pathSeparator);
#4楼
If you mean File.separator
and File.pathSeparator
then: 如果您的意思是File.separator
和File.pathSeparator
那么:
File.pathSeparator
is used to separate individual file paths in a list of file paths.File.pathSeparator
用于分隔文件路径列表中的各个文件路径。 Consider on windows, the PATH environment variable. 在Windows上考虑PATH环境变量。 You use a;
你用的是;
to separate the file paths so on WindowsFile.pathSeparator
would be;
分离文件路径,以便在WindowsFile.pathSeparator
上;
. 。File.separator
is either/
or\\
that is used to split up the path to a specific file.File.separator
是/
或\\
,用于将路径拆分为特定文件。 For example on Windows it is\\
orC:\\Documents\\Test
例如,在Windows上它是\\
或C:\\Documents\\Test