Java File.separator 与 File.pathSeparator

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27746419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:57:36  来源:igfitidea点击:

File.separator vs. File.pathSeparator

javafileseparatordifferencepath-separator

提问by Evorlor

File has the static Strings separatorand pathSeparator. The separator is a "default name-separator character" and the pathSeparator is a "path-separator character".

文件具有静态字符串分隔符路径分隔符。分隔符是“默认名称分隔符”,路径分隔符是“路径分隔符”。

What is the difference? Is there a time when one is preferable to the other?

有什么不同?有没有一个比另一个更可取的时候?

采纳答案by foxt7ot

java.io.File class contains four static separator variables. For better understanding, Let's understand with the help of some code

java.io.File 类包含四个静态分隔符变量。为了更好的理解,让我们借助一些代码来理解

  1. separator: Platform dependent default name-separator character as String. For windows, it's ‘\' and for unix it's ‘/'
  2. separatorChar: Same as separator but it's char
  3. pathSeparator: Platform dependent variable for path-separator. For example PATH or CLASSPATH variable list of paths separated by ‘:' in Unix systems and ‘;' in Windows system
  4. pathSeparatorChar: Same as pathSeparator but it's char
  1. 分隔符:依赖于平台的默认名称分隔符作为字符串。对于 Windows,它是“\”,对于 unix,它是“/”
  2. separatorChar:与分隔符相同,但它是字符
  3. pathSeparator:路径分隔符的平台因变量。例如 PATH 或 CLASSPATH 路径的变量列表,在 Unix 系统中由 ':' 和 ';' 分隔 在 Windows 系统中
  4. pathSeparatorChar:与 pathSeparator 相同,但它是 char

Note that all of these are final variables and system dependent.

请注意,所有这些都是最终变量并且取决于系统。

Here is the java program to print these separator variables. FileSeparator.java

这是打印这些分隔符变量的java程序。文件分隔符.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");