创建绝对路径

huangapple go评论59阅读模式
英文:

Creating an absolute path

问题

Java的Paths类有一个用于创建Path对象的实用方法:

Path path = Paths.get("a", "b", "c", "d", "e.txt");

然而,使用这种方法得到的路径是相对于调用目录的。获取绝对路径的最佳跨平台方法是什么(既能透明地结合Windows的\约定和Unix的/约定)?

英文:

The Java Paths class has this useful method for creating a Path object:

Path path = Paths.get("a", "b", "c", "d", "e.txt");

However, with this approach the resultant path is relative to the invoking directory. What is the best platform-independent way to get an absolute path (one which transparently incorporates both the Windows \ and the Unix / conventions)?

答案1

得分: 1

如果您有一个需要变成绝对路径的 Path 对象,您只需在其上调用 toAbsolutePath() 方法;请注意,路径对象知道平台分隔符是什么;它已经是平台无关的,无需手动转换任何斜杠。

如果您的意思是:我有一堆字符串,在 Unix 上我希望得到表示 /a/b/c/d/e.txt 的路径,但在 Windows 上我希望得到表示 C:\a\b\c\d\e.txt 的路径,那是个问题。因为 C: 实际上不是您可以随意假设的内容。

您的问题无法回答:事实证明,如果您想要完全平台无关,就不存在所谓的 '根目录' 概念。在 Windows 上,只能谈论 '一个根目录',而且与驱动器号关联的文件系统有很多个,这也是一个棘手的抽象,因为实际上 Windows 使用的是 \\驱动器标识符\路径 模型。

您可能想要调查一下这个:

for (Path p : FileSystems.getDefault().getRootDirectories()) {
    System.out.println("发现一个根目录:" + p);
}

例如,您可以选择 '就用第一个根目录,不管是什么':

Path root = FileSystems.getDefault().getRootDirectories().iterator().next();
Path p = root.resolve(Paths.get("a", "b", "c", "d", "e.txt"));
System.out.println(p);

无论这是否是您打算的根目录(可能是 C:),这将取决于相当多的因素。

最好的做法是不要将路径分成不同的字符串。如果您正在接受表示绝对路径的用户输入,请使用一个字符串。Windows 上的用户可能会输入类似 C:\Users\Sandeep\hello.txt 的内容,而 Paths.get() 对于这种输入没有任何问题。Mac 上的用户可能会输入 /Users/Sandeep/hello.txt,如果将其提供给 Path,从那时开始的操作已经完全是平台无关的。

英文:

If you have a Path object that needs to be an absolute path instead, you can just invoke toAbsolutePath() on it; note that path objects know what the platform separator is; it is already platform independent, there's no need to manually convert any slashes.

If you mean: I have a bunch of strings and I want on unix to obtain a path representing /a/b/c/d/e.txt but on windows to obtain a path representing C:\a\b\c\d\e.txt that's a problem. Because C: is not actually something you just get to assume.

Your question is then unanswerable: Turns out that if you want to be fully platform independent, there is no such notion as 'the root directory'. On windows, one can only speak of 'a root directory', and there are as many as file systems hooked up to a drive letter, and that too is a tricky abstraction, because really windows works with a \\drive-identifier\path model.

Something you may want to investigate:

for (Path p : FileSystems.getDefault().getRootDirectories()) {
    System.out.println("Found a root: " + p);
}

You could for example go with 'just go off of the first root, whatever it is':


Path root = FileSystems.getDefault().getRootDirectories().iterator().next();
Path p = root.resolve(Paths.get("a", "b", "c", "d", "e.txt"));
System.out.println(p);

Whether that is the root you intended (presumably, C:) - that'll depend on quite a few factors.

The best thing to do is to forget about having separate strings. If you are taking user input representing an absolute path, take one, single string. A user on windows is bound to type something like: C:\Users\Sandeep\hello.txt, and Paths.get() has no problem with that input at all. A user on mac might type /Users/Sandeep/hello.txt and this too works just fine if you feed it to Path, and operations from there on out are entirely platform independent already.

huangapple
  • 本文由 发表于 2020年7月24日 23:28:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63076700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定