Windows和Linux中用于Java代码的路径分隔符 [英] Path delimiter in windows and linux for java code

查看:361
本文介绍了Windows和Linux中用于Java代码的路径分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Java代码中,我有一些硬编码的路径,我写为

In my java code, I have some hard coded paths which I have written as

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

initPathtmpPath是通过File.getParent()获得的.现在,它可以在Windows上运行,并且如果我将代码移至linux,则\\将会有问题,因为其他两个是由系统方法确定的.结果是这样的

the initPath and tmpPath are obtained by File.getParent(). Now, that works on windows and if I move my code to linux, the \\ will be problematic since the other two are determined by system methods. The results is something like this

/home/mahmood/project/alpha\temp1

我该如何解决?我不想在Linux系统的代码中放入/.

How can I fix that? I don't want to put / in my code for linux systems.

推荐答案

您可以使用一个变量:

There is a variable you can use: File.separator

取决于系统的默认名称分隔符,为方便起见,用字符串表示.初始化此字段以包含系统属性file.separator值的第一个字符.在UNIX系统上,此字段的值为'/';在Microsoft Windows系统上,它是'\'.

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;

这篇关于Windows和Linux中用于Java代码的路径分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆