System.IO.Path.Combine()
在 C#/.NET 中是否有 Java 等价物?或者任何代码来完成这个?
这种静态方法将一个或多个字符串组合成一个路径。
您应该使用旨在表示文件系统路径的类,而不是让所有内容都基于字符串。
如果您使用的是 Java 7 或 Java 8,您应该强烈考虑使用java.nio.file.Path
; Path.resolve
可用于将一条路径与另一条路径或与字符串组合。Paths
助手类也很有用。例如:
Path path = Paths.get("foo", "bar", "baz.txt");
如果您需要满足 Java-7 之前的环境,您可以使用java.io.File
,如下所示:
File baseDirectory = new File("foo");
File subDirectory = new File(baseDirectory, "bar");
File fileInDirectory = new File(subDirectory, "baz.txt");
如果您希望稍后将其作为字符串返回,您可以调用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 7 中,您应该使用resolve
:
Path newPath = path.resolve(childPath);
虽然 NIO2 Path 类对于具有不必要不同 API 的 File 来说似乎有点多余,但实际上它更加优雅和健壮。
请注意,Paths.get()
(正如其他人所建议的那样)没有过载 a Path
,并且做Paths.get(path.toString(), childPath)
与. 不同resolve()
。从Paths.get()
文档:
请注意,虽然此方法非常方便,但使用它意味着对默认 FileSystem 的假定引用并限制调用代码的实用程序。因此,不应在旨在灵活重用的库代码中使用它。更灵活的替代方法是使用现有的 Path 实例作为锚点,例如:
Path dir = ... Path path = dir.resolve("file");
to 的姊妹功能resolve
非常出色relativize
:
Path childPath = path.relativize(newPath);
主要答案是使用 File 对象。但是Commons IO确实有一个类FilenameUtils可以做这种事情,例如concat()方法。
平台无关的方法(使用 File.separator,即工作取决于运行代码的操作系统:
java.nio.file.Paths.get(".", "path", "to", "file.txt")
// relative unix path: ./path/to/file.txt
// relative windows path: .\path\to\filee.txt
java.nio.file.Paths.get("/", "path", "to", "file.txt")
// absolute unix path: /path/to/filee.txt
// windows network drive path: \\path\to\file.txt
java.nio.file.Paths.get("C:", "path", "to", "file.txt")
// absolute windows path: C:\path\to\file.txt
我知道自从乔恩的原始答案以来已经有很长时间了,但我对 OP 有类似的要求。
通过扩展 Jon 的解决方案,我想出了以下方法,它将采用一个或多个路径段,并采用您可以扔给它的尽可能多的路径段。
用法
Path.combine("/Users/beardtwizzle/");
Path.combine("/", "Users", "beardtwizzle");
Path.combine(new String[] { "/", "Users", "beardtwizzle", "arrayUsage" });
在这里为其他有类似问题的人编写代码
public class Path {
public static String combine(String... paths)
{
File file = new File(paths[0]);
for (int i = 1; i < paths.length ; i++) {
file = new File(file, paths[i]);
}
return file.getPath();
}
}
为了增强 JodaStephen 的回答,Apache Commons IO 具有执行此操作的 FilenameUtils。示例(在 Linux 上):
assert org.apache.commons.io.FilenameUtils.concat("/home/bob", "work\\stuff.log") == "/home/bob/work/stuff.log"
它是独立于平台的,可以生成系统需要的任何分隔符。
也许迟到了,但我想分享我对此的看法。我正在使用 Builder 模式并允许方便地链接append(more)
调用,并允许混合File
和String
. 它也可以很容易地扩展以支持使用Path
对象,并且还可以在 Linux、Macintosh 等平台上正确处理不同的路径分隔符。
public class Files {
public static class PathBuilder {
private File file;
private PathBuilder ( File root ) {
file = root;
}
private PathBuilder ( String root ) {
file = new File(root);
}
public PathBuilder append ( File more ) {
file = new File(file, more.getPath()) );
return this;
}
public PathBuilder append ( String more ) {
file = new File(file, more);
return this;
}
public File buildFile () {
return file;
}
}
public static PathBuilder buildPath ( File root ) {
return new PathBuilder(root);
}
public static PathBuilder buildPath ( String root ) {
return new PathBuilder(root);
}
}
使用示例:
File root = File.listRoots()[0];
String hello = "hello";
String world = "world";
String filename = "warez.lha";
File file = Files.buildPath(root).append(hello).append(world)
.append(filename).buildFile();
String absolute = file.getAbsolutePath();
结果absolute
将包含以下内容:
/hello/world/warez.lha
甚至可能:
A:\hello\world\warez.lha
如果您不需要多个字符串,则可以使用com.google.common.io.Files
Files.simplifyPath("some/prefix/with//extra///slashes" + "file//name")
要得到
"some/prefix/with/extra/slashes/file/name"
这是一个处理多个路径部分和边缘条件的解决方案:
public static String combinePaths(String ... paths)
{
if ( paths.length == 0)
{
return "";
}
File combined = new File(paths[0]);
int i = 1;
while ( i < paths.length)
{
combined = new File(combined, paths[i]);
++i;
}
return combined.getPath();
}
该解决方案提供了一个接口,用于连接来自 String[] 数组的路径片段。它使用java.io.File.File(String parent, String child):
public static joinPaths(String[] fragments) {
String emptyPath = "";
return buildPath(emptyPath, fragments);
}
private static buildPath(String path, String[] fragments) {
if (path == null || path.isEmpty()) {
path = "";
}
if (fragments == null || fragments.length == 0) {
return "";
}
int pathCurrentSize = path.split("/").length;
int fragmentsLen = fragments.length;
if (pathCurrentSize <= fragmentsLen) {
String newPath = new File(path, fragments[pathCurrentSize - 1]).toString();
path = buildPath(newPath, fragments);
}
return path;
}
然后你可以这样做:
String[] fragments = {"dir", "anotherDir/", "/filename.txt"};
String path = joinPaths(fragments);
回报:
"/dir/anotherDir/filename.txt"
假设所有给定路径都是绝对路径。您可以按照以下片段合并这些路径。
String baseURL = "\\\\host\\testdir\\";
String absoluteFilePath = "\\\\host\\testdir\\Test.txt";;
String mergedPath = Paths.get(baseURL, absoluteFilePath.replaceAll(Matcher.quoteReplacement(baseURL), "")).toString();
输出路径是 \\host\testdir\Test.txt。
这也适用于 Java 8:
Path file = Paths.get("Some path");
file = Paths.get(file + "Some other path");