Jon*_*eet 385
您应该使用一个旨在表示文件系统路径的类,而不是保持所有基于字符串的类.
如果您使用的是Java 7或Java 8,则应该强烈考虑使用java.nio.file.Path; Path.resolve可用于将一个路径与另一个路径或字符串组合在一起.该Paths辅助类是有用的.例如:
Path path = Paths.get("foo", "bar", "baz.txt");
Run Code Online (Sandbox Code Playgroud)
如果您需要满足Java-7之前的环境,您可以使用java.io.File,如下所示:
File baseDirectory = new File("foo");
File subDirectory = new File(baseDirectory, "bar");
File fileInDirectory = new File(subDirectory, "baz.txt");
Run Code Online (Sandbox Code Playgroud)
如果你以后想把它作为一个字符串,你可以打电话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();
}
Run Code Online (Sandbox Code Playgroud)
Ale*_*sky 105
在Java 7中,您应该使用resolve:
Path newPath = path.resolve(childPath);
Run Code Online (Sandbox Code Playgroud)
虽然NIO2 Path类对于带有不必要的API的File来说似乎有点多余,但它实际上更加优雅和健壮.
请注意Paths.get()(正如其他人所建议的那样)没有超载Path,并且做Paths.get(path.toString(), childPath)的事与此不同resolve().来自Paths.get()文档:
请注意,虽然此方法非常方便,但使用它将意味着对默认FileSystem的假定引用并限制调用代码的实用程序.因此,它不应该在用于灵活重用的库代码中使用.更灵活的替代方法是使用现有的Path实例作为锚点,例如:
Run Code Online (Sandbox Code Playgroud)Path dir = ... Path path = dir.resolve("file");
姐妹的功能resolve是优秀的relativize:
Path childPath = path.relativize(newPath);
Run Code Online (Sandbox Code Playgroud)
Jod*_*hen 42
主要答案是使用File对象.但是Commons IO确实有一个类FilenameUtils可以做这种事情,比如concat()方法.
isN*_*247 17
我知道自Jon的原始答案以来很长一段时间,但我对OP有类似的要求.
通过扩展Jon的解决方案,我提出了以下内容,它将采用一个或多个路径段,从而可以为其提供尽可能多的路径段.
用法
Path.combine("/Users/beardtwizzle/");
Path.combine("/", "Users", "beardtwizzle");
Path.combine(new String[] { "/", "Users", "beardtwizzle", "arrayUsage" });
Run Code Online (Sandbox Code Playgroud)
此处为具有类似问题的其他人编码
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();
}
}
Run Code Online (Sandbox Code Playgroud)
Mak*_*min 12
平台无关的方法(使用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
Run Code Online (Sandbox Code Playgroud)
alp*_*ian 11
为了增强JodaStephen的答案,Apache Commons IO有FilenameUtils这样做.示例(在Linux上):
assert org.apache.commons.io.FilenameUtils.concat("/home/bob", "work\\stuff.log") == "/home/bob/work/stuff.log"
Run Code Online (Sandbox Code Playgroud)
它独立于平台,可以生成系统所需的任何分隔符.
也许聚会迟到了,但我想分享我对此的看法。我不喜欢为这样的事情引入整个库。相反,我使用构建器模式并允许方便的链式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);
}
}
Run Code Online (Sandbox Code Playgroud)
使用示例:
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();
Run Code Online (Sandbox Code Playgroud)
结果absolute将包含类似以下内容:
/hello/world/warez.lha
Run Code Online (Sandbox Code Playgroud)
或者甚至可能:
A:\hello\world\warez.lha
Run Code Online (Sandbox Code Playgroud)