java - Windows 和 Linux 中用于 Java 代码的路径定界符

标签 java windows filepath

在我的 java 代码中,我有一些硬编码的路径,我写成

String workingPath = initPath + "\\" + tmpPath;

initPathtmpPath 是通过File.getParent() 获取的。现在,这适用于 Windows,如果我将我的代码移至 linux,\\ 将出现问题,因为其他两个由系统方法确定。结果是这样的

/home/mahmood/project/alpha\temp1

我该如何解决?我不想将 / 放在我的 linux 系统代码中。

最佳答案

您可以使用一个变量:File.separator

The system-dependent default name-separator character, represented as a string for convenience. This field is initialized to contain the first character of the value of the system property file.separator. On UNIX systems the value of this field is '/'; on Microsoft Windows systems it is '\'.

String workingPath = initPath + File.separator + tmpPath;

关于java - Windows 和 Linux 中用于 Java 代码的路径定界符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44250017/

相关文章:

java - 项目中使用的设计模式

Java - getResource 在 csv 文件上失败

swift - URLforResource 始终返回 nil

windows - Windbg 和符号文件

c++ - Windows API : Detecting when a driver install has finished

c# - 以编程方式从网络接口(interface)发送数据

java - 如何从包含绝对文件路径的字符串中获取文件名?

java - 使用接口(interface)将一个数组列表传递给包装类中的另一个数组列表

java - 如何链接 TextView 中的文本以打开网址

java - 我应该在基于 Struts 2.0.11 的 Web 应用程序中使用什么调度程序?