在我的 java 代码中,我有一些硬编码的路径,我写成
String workingPath = initPath + "\\" + tmpPath;
initPath
和tmpPath
是通过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/