本文翻译自: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

File.separator或File.pathSeparator相关推荐

  1. File.separator

    报告"No such file or diretory "的异常,上传不了.后来发现是文件路径的问题.模拟测试环境是windows+tomcat,而正式的的环境是linux+tom ...

  2. 关于File.separator 文件路径:wind与linux下路径问题 .

    最近有个在页面上传Excel文件至服务器指定目录并进行数据校验.最后入库及进行进一步处理的应用情境,我写好代码在模拟环境下测试,完全没问题:但客户试用的时候,却老是报告"No such fi ...

  3. Java:File.separator作用相当于 ‘ \ ‘

    其实 File.separator 的作用相当于 ' \ ' 在 windows 中 文件文件分隔符用 ' \ ' 或者 ' / ' 都可以 但是在 Linux 中,是不识别 ' \ ' 的,而 Fi ...

  4. 关于File.separator 文件路径:window与linux下路径问题(“No such file or diretory ”异常解决方案)...

    最近有个在页面上传Excel文件至服务器指定目录并进行数据校验.最后入库及进行进一步处理的应用情境,我写好代码在模拟环境下测试,完全没问题:但客户试用的时候,却老是报告"No such fi ...

  5. 关于File.separator[转]

    写好代码在模拟环境下测试,完全没问 题:但linux+tomcat下用的时候,却老是报告"No such file or diretory "的异常,上传不了.后来发现是文件路径的 ...

  6. File.separator是什么?

    File.separator是什么? 注意: 其实 File.separator 的作用相当于 ' \ '在 windows 中 文件文件分隔符 用 ' \ ' 或者 ' / ' 都可以但是在 Lin ...

  7. Java的File.separator

    在Windows下的路径分隔符和Linux下的路径分隔符是不一样的,当直接使用绝对路径时,跨平台会暴出"No such file or diretory"的异常. 比如说要在tem ...

  8. java中separator_java - File.separator和路径中的斜杠之间的区别

    java - File.separator和路径中的斜杠之间的区别 在Java Path-String中使用/和普通的File.separator有什么区别? 与双反斜杠相比,/平台独立似乎不是原因, ...

  9. 关于Java的File.separator

    一.File类 在Windows下的路径分隔符(\)和在Linux下的路径分隔符(/)是不一样的,当直接使用绝对路径时,跨平台会报No Such file or diretory异常. File中还有 ...

最新文章

  1. tomcat1.8,tomcat-users.xml文件中如下修改才可以使用app manager登录,其中的roles有哪些枚举需要确认
  2. XShell与虚拟机连接的IP问题
  3. java valueof的用法_Java SignStyle valueOf()用法及代码示例
  4. sketch软件_Sketch软件怎么用?怎么提升Sketch软件技巧?
  5. python限制输入值范围_求python 中if 里如何设定一个值的范围
  6. ANSYS CFX 脚本详细设置,实现循环计算
  7. 【数据库】pymysql数据库事务操作
  8. DB2表结构DDL脚本导出
  9. 数据库利器Navicat最全快捷键整理
  10. Linux各个版本防火墙操作(CentOS Ubuntu)
  11. vue项目中vue-echarts讲解及常用图表方案实现
  12. System V与Posix
  13. Android 源码结构简介
  14. ECCV 2018 论文下载及分析(774篇全)
  15. Java解压ZIP、RAR文件
  16. pr cpu100%_PR插件Neat Video5.0.2安装教程
  17. js 入门基础(一)
  18. GC日志的查看(日志意思)
  19. 记录一次Win10莫名其妙被植入一个恶意软件
  20. 《文本大数据情感分析》读书报告

热门文章

  1. WMS(一):Window的添加过程
  2. Java IO 体系(二): inputstream与outputstream
  3. Android SystemTrace使用攻略
  4. 第十、十一周项目-阅读程序,写出这些程序的运行结果(2)
  5. 学计算机打字一段话,初学电脑基础知识打字
  6. linux查找nginx目录,Linux下查看nginx安装目录
  7. 文件管理系统_我的文件管理系统
  8. JavaScript内置函数及API
  9. uni-app编译配置
  10. zTree中父节点禁用,子节点可以用