我正在撰寫一種將檔案從 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?
uj5u.com熱心網友回復:
File#separator如果您想保持路徑邏輯與平臺無關,則沒有必要。一個更簡單的方法是構建子路徑Paths.get()并解決它。例如,:
Path path = Paths.get("a", "b", "c");
Path directory = ...;
Path target = directory.resolve(path);
uj5u.com熱心網友回復:
我正在添加我自己的答案。
// 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。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/310816.html
