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

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

问题描述

Java 中的组合路径

我想知道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");

推荐答案

这涉及 Java 7 及更早版本.

This concerns Java versions 7 and earlier.

引用同一问题的好答案:

如果您稍后希望将其作为字符串返回,您可以调用 getPath().事实上,如果你真的想模仿 Path.Combine,你可以这样写:

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天全站免登陆