问题
###确切重复:
combine paths in java
我想知道Java中是否有这样的方法。以此片段为例:
// this will output a/b
System.out.println(path_join("a","b"));
// a/b
System.out.println(path_join("a","/b");
#1 热门回答(157 赞)
这涉及Java版本7及更早版本。
引用agood answer to the same question:
如果以后要将其作为字符串返回,可以调用getPath()。事实上,如果你真的想模仿Path.Combine,你可以写下这样的东西:
public static String combine (String path1, String path2) {
File file1 = new File(path1);
File file2 = new File(file1, path2);
return file2.getPath();
}
#2 热门回答(97 赞)
你可以这样做
String joinedPath = new File(path1, path2).toString();
#3 热门回答(0 赞)
一种方法是获取系统属性,为你提供操作系统的路径分隔符,this tutorial解释如何。然后,你可以使用file.separator
使用标准字符串连接。