当“点目录"出现时,Path.relativize行为.已经包括了 [英] Path.relativize behaviour when "dot directory" is included
问题描述
关于您可以阅读的Path.relativize
方法
[...]此方法尝试构造一个相对路径,当 对此路径解析,产生一个找到相同文件的路径 作为给定的路径.例如,在UNIX上,如果此路径为"/a/b",并且 给定的路径为"/a/b/c/d",则所得的相对路径为 光盘". [...]
[...] This method attempts to construct a relative path that when resolved against this path, yields a path that locates the same file as the given path. For example, on UNIX, if this path is "/a/b" and the given path is "/a/b/c/d" then the resulting relative path would be "c/d". [...]
所以
Path p1 = Paths.get("/a/b");
Path p2 = Paths.get("/a/b/c/d");
System.out.println(p1.relativize(p2));
输出
c\d
只要我了解整个想法就好比问我目前在路径p1
所给的位置,如何从这里到达路径p2
?" .
As long as I understand the whole idea is like asking "I'm currently in the location given by path p1
, how do I get to path p2
from here?".
但是对我来说,鉴于/a/b
和/a/b/.
表示相同的位置,我希望上述问题的答案是相同的,而
But to me, given that /a/b
and /a/b/.
represent the same location, I'd expect the answer to the above question to be the same, while
Path p1 = Paths.get("/a/b/.");
Path p2 = Paths.get("/a/b/c/d");
System.out.println(p1.relativize(p2));
打印
..\c\d
相反.在这种情况下,如果我在p1
给出的位置,则应用..\c\d
我将不会进入p2
而是会进入\a\c\d
,所以...要么有些矛盾,要么我的隐喻是错误的.那么,您能帮我理解我的隐喻在哪里以及为什么错了吗?
instead. In this case, if I'm in the location given by p1
, applying ..\c\d
I won't get to p2
but to \a\c\d
, so... Either there's some inconsistency or my metaphore is wrong. So, could you help me to understand where and why is my metaphore wrong?
推荐答案
出于p1
的目的,路径段.
被视为另一个名称,而不是当前目录的特殊情况.
The path segment .
, for the purposes of p1
, is considered just another name, not a special case for the current directory.
如果要表示当前目录,请 normalize
路径.
If you want it to mean the current directory, normalize
the path.
此方法的确切定义取决于实现,但 通常,它从此路径派生,该路径不包含 多余的名称元素.在许多文件系统中,
"."
和".."
是 特殊名称,用于指示当前目录和父目录 目录. 在这样的文件系统中,所有出现的"."
都被认为是 .如果".."
前面带有非".."
名称,则两个名称 被认为是多余的(识别此类名称的过程是 重复,直到不再适用为止.
The precise definition of this method is implementation dependent but in general it derives from this path, a path that does not contain redundant name elements. In many file systems, the
"."
and".."
are special names used to indicate the current directory and parent directory. In such file systems all occurrences of"."
are considered redundant. If a".."
is preceded by a non-".."
name then both names are considered redundant (the process to identify such names is repeated until it is no longer applicable).
这篇关于当“点目录"出现时,Path.relativize行为.已经包括了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!