完全相同的副本:
我想知道Java中是否有这样的方法。以下面的代码片段为例:
// this will output a/b
System.out.println(path_join("a","b"));
// a/b
System.out.println(path_join("a","/b");
发布于 2009-04-02 23:15:08
这与Java版本7及更早版本有关。
引用good 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();
}
发布于 2009-04-03 06:15:04
尝试:
String path1 = "path1";
String path2 = "path2";
String joinedPath = new File(path1, path2).toString();
发布于 2009-04-02 23:15:57
一种方法是获取系统属性,为您提供操作系统的路径分隔符,this tutorial解释了如何做到这一点。然后,您可以通过file.separator
使用标准字符串联接。
https://stackoverflow.com/questions/711993
复制相似问题