Java有一个路径连接方法吗? [英] Does Java have a path joining method?

查看:132
本文介绍了Java有一个路径连接方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

完全重复:



在java中合并路径 p>

我想知道在Java中是否有这样的方法。以这个片段为例:

$ p $ //这将输出一个/ b
System.out.println(path_join 一, b));
// a / b
System.out.println(path_join(a,/ b);


解决方案

引用对同一个问题的好的答案

如果你以后需要返回字符串,可以调用getPath()。真的很想模仿Path.Combine,你可以写一些东西:

  public static String combine(String path1,String path2) {
文件file1 =新文件(path1);
文件file2 =新文件(file1,path2);
返回file2.getPath();
}


Exact Duplicate:

combine paths in java

I would like to know if there is such a method in Java. Take this snippet as example :

// this will output a/b
System.out.println(path_join("a","b"));
// a/b 
System.out.println(path_join("a","/b");

解决方案

To quote a good answer to the same question:

If you want it back as a string later, you can call getPath(). Indeed, if you really wanted to mimic Path.Combine, you could just write something like:

public static String combine (String path1, String path2) {
    File file1 = new File(path1);
    File file2 = new File(file1, path2);
    return file2.getPath();
}

这篇关于Java有一个路径连接方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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