简体   繁体   English

如何使路径名与不同的操作系统兼容?

[英]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.separatorFile.separatorChar
Check the File class javadoc for more info on this. 检查File类javadoc以获得更多信息。

edit: Difference between separator and pathSeparator . 编辑: separatorpathSeparator之间的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.

相关问题 如何使JNDI名称与GlassFish和WildFly兼容 - How do I make JNDI names compatible with both GlassFish and WildFly 如何对兼容的通配符进行不同的捕获? - How can I make different captures of a wildcard compatible? 如何确保java程序与Java Enviornment 6兼容? - How do I make sure that a java program is compatible with Java Enviornment 6? 方法引用如何与具有不同实现功能名称的功能接口兼容? - How is a Method reference compatible with functional interface with different implementing function names? 如何在不同的操作系统上部署Swing应用程序? - How to deploy Swing application on different OSs? 如何使文件路径与嵌入式数据库兼容? (Apache Derby 嵌入式) - How to make a file path compatible with an embedded database? (Apache Derby Embedded) 我如何使这个Java 7兼容? - How would I make this Java 7 compatible? 如何在while循环中保存具有不同名称的ArrayList? - How do I save ArrayLists with different names in a while loop? 如何将字符串与不同的名称一起添加 - How do I add strings together with different names 在不兼容的类型问题上得到了.equals(),我如何制作我的 ArrayList<point> 和点兼容,答案是 boolean</point> - got the .equals() on incompatible types problem, how do i make my ArrayList <Point> and point compatible and the answer into an boolean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM