首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java有路径连接方法吗?

Java有路径连接方法吗?
EN

Stack Overflow用户
提问于 2009-04-02 23:12:36
回答 4查看 143.9K关注 0票数 142

完全相同的副本:

combine paths in java

我想知道Java中是否有这样的方法。以下面的代码片段为例:

代码语言:javascript
复制
// this will output a/b
System.out.println(path_join("a","b"));
// a/b 
System.out.println(path_join("a","/b");
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2009-04-02 23:15:08

这与Java版本7及更早版本有关。

引用good answer to the same question

如果您希望稍后将其作为字符串返回,则可以调用getPath()。实际上,如果你真的想模仿Path.Combine,你可以写一些类似这样的代码:

代码语言:javascript
复制
public static String combine (String path1, String path2) {
    File file1 = new File(path1);
    File file2 = new File(file1, path2);
    return file2.getPath();
}
票数 169
EN

Stack Overflow用户

发布于 2009-04-03 06:15:04

尝试:

代码语言:javascript
复制
String path1 = "path1";
String path2 = "path2";

String joinedPath = new File(path1, path2).toString();
票数 109
EN

Stack Overflow用户

发布于 2009-04-02 23:15:57

一种方法是获取系统属性,为您提供操作系统的路径分隔符,this tutorial解释了如何做到这一点。然后,您可以通过file.separator使用标准字符串联接。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/711993

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档