我应该关心 File#separator for Path#resolve(String)
Should I care about File#separator for Path#resolve(String)
我正在编写一个方法,将文件从 HDFS 复制到本地磁盘。
String pathString = ".../.../..."; slash-separated HDFS path string
Path directory = ...;
Path target = directory.resolve(pathString);
现在我应该将所有带有 File#separator
的斜线替换为 pathString
吗?
换句话说,
即使 Windows 我也可以跟随吗?
Path base = Path.get(a, b);
String pathname = "c/d";
Path target = base.resolve(pathname); // Will work as a\b\c\d?
File#separator
如果你想让你的路径逻辑与平台无关,则没有必要。一种更简单的方法是使用 Paths.get()
构建子路径并解决它。例如:
Path path = Paths.get("a", "b", "c");
Path directory = ...;
Path target = directory.resolve(path);
我正在添加我自己的答案。
// event with Windows
final Path directory = Paths.get("a", "b");
final String pathname = "c/d";
final Path resolved = directory.resolve(pathname);
log.debug("resolved: {}", resolved.toAbsolutePath());
我得到了.:\...\a\b\c\d
。
我正在编写一个方法,将文件从 HDFS 复制到本地磁盘。
String pathString = ".../.../..."; slash-separated HDFS path string
Path directory = ...;
Path target = directory.resolve(pathString);
现在我应该将所有带有 File#separator
的斜线替换为 pathString
吗?
换句话说,
即使 Windows 我也可以跟随吗?
Path base = Path.get(a, b);
String pathname = "c/d";
Path target = base.resolve(pathname); // Will work as a\b\c\d?
File#separator
如果你想让你的路径逻辑与平台无关,则没有必要。一种更简单的方法是使用 Paths.get()
构建子路径并解决它。例如:
Path path = Paths.get("a", "b", "c");
Path directory = ...;
Path target = directory.resolve(path);
我正在添加我自己的答案。
// event with Windows
final Path directory = Paths.get("a", "b");
final String pathname = "c/d";
final Path resolved = directory.resolve(pathname);
log.debug("resolved: {}", resolved.toAbsolutePath());
我得到了.:\...\a\b\c\d
。