本文共 3934 字,大约阅读时间需要 13 分钟。
本文翻译自:
In the File
class there are two strings, and . 在File
类中有两个字符串, 和 。
What's the difference? 有什么不同? When should I use one over the other? 我什么时候应该使用另一个?
参考:
java.io.File
class contains four static separator variables. java.io.File
类包含四个静态分隔符变量。 For better understanding, Let's understand with the help of some code 为了更好地理解,让我们在一些代码的帮助下理解
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 systemsFile fileUnsafe = new File("tmp/abc.txt");//platform independent and safe to use across Unix and WindowsFile fileSafe = new File("tmp"+File.separator+"abc.txt");
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);
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 Windows File.pathSeparator
would be ;
分离文件路径,以便在Windows File.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 \\
or C:\\Documents\\Test
例如,在Windows上它是\\
或C:\\Documents\\Test
转载地址:http://ipcnb.baihongyu.com/