最佳答案
在 Python中,我可以用 os.path.join连接两条路径:
os.path.join("foo", "bar") # => "foo/bar"
我试图在 Java 中达到同样的效果,而不用担心 OS是 Unix、 Solaris还是 Windows:
public static void main(String[] args) {
Path currentRelativePath = Paths.get("");
String current_dir = currentRelativePath.toAbsolutePath().toString();
String filename = "data/foo.txt";
Path filepath = currentRelativePath.resolve(filename);
// "data/foo.txt"
System.out.println(filepath);
}
我原以为 Path.resolve( )会加入我的工作目录 /home/user/test和 data/foo.txt一起制作 /home/user/test/data/foo.txt。
我哪里搞错了?