[英]how do I make path names compatible with different OSs?
I used to know it, but I know there is a way in java to make sure the path works in all OSs (Windows uses a different "/" system than Unix based systems). 我曾经知道,但是我知道Java中有一种方法可以确保该路径在所有OS上都能正常工作(Windows使用与基于Unix的系统不同的“ /”系统)。 What do I have to do to create a file path name that works with all systems?
要创建适用于所有系统的文件路径名,我该怎么做?
EDIT: I wasn't sure if this is what I meant, but I think the file path will end up being relative. 编辑:我不确定这是否是我的意思,但是我认为文件路径最终将是相对的。
Also, what's the difference between File.separator and File.pathSeparator ? 另外,File.separator和File.pathSeparator有什么区别?
You'll have to use File.separator
or File.separatorChar
for this purpose. File.separatorChar
,您必须使用File.separator
或File.separatorChar
。
Check the File
class javadoc for more info on this. 检查
File
类javadoc以获得更多信息。
edit: Difference between separator
and pathSeparator
. 编辑:
separator
和pathSeparator
之间的pathSeparator
。 The first separates the folder in a file's path like the /
in /usr/bin
. 第一个在文件路径(如
/usr/bin
的/
中分隔文件夹。 The second one is a separator in a path
environment variable, like the ;
第二个是
path
环境变量中的分隔符,例如;
in PATH=C:/windows/bin;C:/anotherfolder
在
PATH=C:/windows/bin;C:/anotherfolder
You can just use /
(forward slash) as the path separator. 您可以仅使用
/
(正斜杠)作为路径分隔符。 That will work cross-platform. 那将跨平台工作。
File
带有分隔符
除了不确定它的受欢迎程度外,我以前已经使用过它。
System.getProperty("file.separator");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.