File.separator或File.pathSeparator

本文翻译自:File.separator or File.pathSeparator

In the File class there are two strings, separator and pathSeparator . File类中有两个字符串, separatorpathSeparator

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 为了更好地理解,让我们在一些代码的帮助下理解

  1. separator: Platform dependent default name-separator character as String. separator:平台相关的默认名称 - 分隔符字符串。 For windows, it's '\\' and for unix it's '/' 对于Windows,它是'\\'而对于unix它是'/'
  2. separatorChar: Same as separator but it's char separatorChar:与分隔符相同,但它是char
  3. 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系统中
  4. 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.separatorFile.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

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值