博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
File.separator或File.pathSeparator
阅读量:2289 次
发布时间:2019-05-09

本文共 3934 字,大约阅读时间需要 13 分钟。

本文翻译自:

In the File class there are two strings, and . File类中有两个字符串, 和 。

What's the difference? 有什么不同? When should I use one over the other? 我什么时候应该使用另一个?


#1楼

参考:


#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 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");

#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

转载地址:http://ipcnb.baihongyu.com/

你可能感兴趣的文章
罕见bug解决办法: kienct 1代运行错误Failed to claim camera interface: LIBUSB_ERROR_NOT_FOUND
查看>>
Ubuntu 如何设置静态IP
查看>>
systemd的简单使用
查看>>
kinetic opencv cmake.conf 文件的bug修复
查看>>
利用网络传输系统的声音
查看>>
安装Nvidia显卡驱动和CUDA
查看>>
升级ORB_SLAM2依赖程序以提升效率
查看>>
ros系统升级,如何从jade升级到kinetic
查看>>
OpenCV 2.X 和 OpenCV 3.X的区别是什么?
查看>>
android无线调试
查看>>
rviz的简单使用
查看>>
解决ROS的usb_cam节点无法正常读取mjpeg格式摄像头的方法
查看>>
Ubuntu VNC 如何调整分辨率
查看>>
脱壳的艺术--6. 高级及其它技术
查看>>
脱壳的艺术--7. 工具
查看>>
脱壳的艺术--8 参考
查看>>
《脱壳艺术》学习笔记--IsDebuggerPresent 检测调试器
查看>>
《脱壳艺术》学习笔记2--SEH检测调试器
查看>>
经典的重定位代码
查看>>
经典的重定位代码
查看>>