java - Java有路径连接方法吗?

标签 java file file-io path

完全重复:

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");

最佳答案

这涉及 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();
}

关于java - Java有路径连接方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/711993/

相关文章:

java - B树实现

java - Web 应用程序中的 Spring 应用程序上下文刷新

c - 如何避免在 dup2 函数中关闭 newfd?

python - 从大文件(~2.5GB)读取并存储到 python 列表时出现 MemoryError

java - 打印和写入文件时数据不同

java - 如何编写带有条件的 xpath 以获得满足我要求的所需网络元素列表

java - "xx free bytes and xx until OOM"是什么意思?

java - 搜索在特定时间完成

file - 删除 6 个月前的文件

javascript - Node js写入文件