Java 完全合格的路径 Vs。规范路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3598407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fully qualified path Vs. Canonical Path
提问by Benjamin
There is a canonical path concept in Java.
And there is a fully-qualified path in WinApi.
Java 中有一个规范的路径概念。
并且在 WinApi 中有一个完全限定的路径。
I know well what canonical path is, but I don't understand fully-qualified path's concepts.
我很清楚规范路径是什么,但我不理解完全限定路径的概念。
For a file or directory, does fully-qulified path exist only one thing? -like canonical path.
对于文件或目录,完全限定路径是否只存在一件事?- 类似规范路径。
Are both of them totally same concepts?
两者是完全相同的概念吗?
Edit:
One more thing,
Is a symbloc link or a hard link belong to Fully qualified path?
编辑:还有
一件事,符号链接还是硬链接属于完全限定路径?
Edit
I asked someone who maintains Naming Files, Paths, and Namespacespage to let me know this.
And he replied me.
编辑
我问维护命名文件、路径和命名空间页面的人让我知道这一点。
然后他回复了我。
Is this also Fully-qualified path?
C:\directory\..\directory\file.txt
这也是完全限定路径吗?
C:\目录\..\目录\file.txt
Technically that is a relative path because it contains the double dot (..) and some APIs do not process those correctly (the docs will clearly state that it needs a fully qualified path).
The two are mutually exclusive.
从技术上讲,这是一个相对路径,因为它包含双点 (..) 并且某些 API 无法正确处理这些(文档将明确说明它需要一个完全限定的路径)。
两者是相互排斥的。
What he means is, in my guessing, if we put a param like this "C:\directory\..\directory\file.txt" to the function required fully-qualified path, the function never reinterpret the path and then fails.
他的意思是,在我的猜测中,如果我们将这样的参数“C:\directory\..\directory\file.txt”添加到函数所需的完全限定路径中,该函数永远不会重新解释路径然后失败。
If so, fully-qualified path is totally same with an canonical path. Isn't it.
如果是这样,完全限定路径与规范路径完全相同。是不是。
采纳答案by Bert F
"Fully-qualified path"is synonymous with "absolute path"
“完全限定路径”与“绝对路径”同义
- "Fully-qualified"and "absolute path"mean the same thing - a path that is not relative to an implied or specified context.
- Every path is either a fully-qualified pathor else it is a relative path
Every location on a file system has a multitude of paths that could be used to refer to it, including numerous fully-qualified paths:
- C:\temp.txt
- C:\Program Files\..\temp.txt
- C:\Program Files\Microsoft\..\..\temp.txt
- etc.
Conceptually speaking, one of those fully-qualified pathsis the simplest, most straightforward way of specifying that resource - that's your canonical path.
- “完全限定”和“绝对路径”是同一个东西——一条与隐含或指定上下文无关的路径。
- 每条路径要么是完全限定路径,要么是相对路径
文件系统上的每个位置都有许多可用于引用它的路径,包括许多完全限定的路径:
- C:\temp.txt
- C:\Program Files\..\temp.txt
- C:\Program Files\Microsoft\..\..\temp.txt
- 等等。
从概念上讲,这些完全限定的路径之一是指定该资源的最简单、最直接的方法 - 这就是您的规范路径。
For a file or directory, does fully-qulified path exist only one thing? -like canonical path.
对于文件或目录,完全限定路径是否只存在一件事?- 类似规范路径。
No, a fully-qualified path is any path which is not a relative path (not relative to the current directory of the implied or specified context). Multiple, but distinct, fully-qualified paths could refer to the same location on the filesystem. Reread:
不,完全限定路径是任何不是相对路径的路径(不相对于隐含或指定上下文的当前目录)。多个但不同的完全限定路径可以引用文件系统上的相同位置。重读:
What's the difference between getPath(), getAbsolutePath(), and getCanonicalPath() in Java?
Java 中的 getPath()、getAbsolutePath() 和 getCanonicalPath() 之间有什么区别?
but substitute "fully-qualified" everywhere it says "absolute".
但在所有说“绝对”的地方替换为“完全合格”。
To be clear, some people will also use the term "relative path" to also refer to a path with a "relative reference" (double dots ..
) within it. For example, some might might called C:\Program Files\Microsoft\\..\temp.txt
a "relative path" because of the double dots, but I would call it an fully-qualified path with a relative reference. Hopefully, it will be clear from the conversation what they mean when they say "relative path" (a path that is relative to a context or a path with a relative reference in it).
需要明确的是,有些人还会使用术语“相对路径”来指代其中包含“相对参考”(双点..
)的路径。例如,C:\Program Files\Microsoft\\..\temp.txt
由于双点,有些人可能将其称为“相对路径”,但我会将其称为具有相对引用的完全限定路径。希望从对话中可以清楚他们说“相对路径”(与上下文相关的路径或其中包含相对引用的路径)是什么意思。
Are both of them totally same concepts?
两者是完全相同的概念吗?
No, as indicated in the other SO question, there are lots ways to specify a fully-qualified path (absolute path) to a location, but only one of those fully-qualified paths is considered to be the canonical path to that location.
不,如另一个 SO 问题所示,有很多方法可以指定到某个位置的完全限定路径(绝对路径),但只有其中一个完全限定路径被认为是到该位置的规范路径。
One more thing, Is a UNC path belong to fully-qualified path too?
还有一件事,UNC 路径是否也属于完全限定路径?
Yes, UNC paths are not relative paths; they are fully-qualified paths. - http://msdn.microsoft.com/en-us/library/aa365247(v=VS.85).aspx#fully_qualified_vs._relative_paths
是的,UNC 路径不是相对路径;它们是完全限定的路径。- http://msdn.microsoft.com/en-us/library/aa365247(v=VS.85).aspx#fully_qualified_vs._relative_paths
Is a symbolic link or a hard link belong to Fully qualified path?
符号链接还是硬链接属于完全限定路径?
Its an independent concept. A path (regardless of whether it is relative or full-qualified) leads to a location in the filesystem. The entity at that location could be one of many things: a normal file, a directory, a symbolic link, a hard link, a device, a named pipe, etc. A symbolic links or a hard link has meta-data that leads to the data you were actually looking for at that location.
它是一个独立的概念。一个路径(不管它是相对的还是完全限定的)通向文件系统中的一个位置。该位置的实体可以是许多事物之一:普通文件、目录、符号链接、硬链接、设备、命名管道等。符号链接或硬链接具有导致您在该位置实际查找的数据。
Analogy Time
类比时间
You can think of paths and links in the terms of directions to someone's house:
您可以根据通往某人房屋的方向来考虑路径和链接:
- a relative pathis directions from your current location
- a fully-qualified pathis directions from town-hall, regardless of where you are
- In our strange little town of Unixville, everyone agrees and understands implicitly that "fully-qualified directions" always start at town-hall, strangely enough, a buidling that everyone calls "
/
". - The next town over (Windowsville) has multiple town halls (one for each part of town), called
C:\
,D:\
,E:\
, etc. - Different people might give you different directions (paths) to get to the same house, even if they all start from the same starting point (townhall) - some directions will be more direct than others.
- In our strange little town of Unixville, everyone agrees and understands implicitly that "fully-qualified directions" always start at town-hall, strangely enough, a buidling that everyone calls "
- a canonical pathis the fully-qualified directions that is the simplest, most straightforward means to get from townhall to the desired house
- a symbolic linkis like a empty lot with a note that gives directions to a forwarding address
- the type of directions that led you here (whether they were relative directions, fully-qualified directions, or even the canonical fully-qualified directions) has no bearing on whether it leads to a house or any empty lot with forwarding direction here
- there's a strange case where one of the streets in your direction is actually a symbolic link (a detour? a portal?) - the analogy falls apart here if we look too closely at it, so lets just ignore it :-)
- a hard linkis a house accessible from two or more different addresses.
- Think of a house on the corner of Elm Street and Main Street. The post office mistakenly gave it two addresses :
10 Elm Str
and20 Main Str
. No matter which address you go to, you end up at the same house. - In our strange little town, these hard-link houses can have multiple addresses and the addresses don't have to be anywhere near each other.
- No matter which of its addresses you go to, its the same house. Its not a copy, its not a forwarding address. Just magically, once you go inside, you end up in the same house, regardless of which address you used to get there.
- the directions that led you to the house (no matter which address was used or whether the directions were relative directions, fully-qualified directions, or even the canonical fully-qualified directions) has no bearing on whether the house at that address is a hard-link house or not
- Think of a house on the corner of Elm Street and Main Street. The post office mistakenly gave it two addresses :
- 一个相对路径是从您当前所在位置
- 一条完全合格的路径是来自市政厅的方向,无论您身在何处
- 在我们陌生的Unixville小镇,每个人都含蓄地同意和理解“完全合格的方向”总是从市政厅开始,奇怪的是,一个每个人都称之为“
/
”的建筑。 - 下一个城镇以上(Windowsville)具有多个会堂(一个用于镇各部分),称为
C:\
,D:\
,E:\
等。 - 不同的人可能会给你不同的方向(路径)去同一个房子,即使他们都从同一个起点(市政厅)开始——有些方向会比其他方向更直接。
- 在我们陌生的Unixville小镇,每个人都含蓄地同意和理解“完全合格的方向”总是从市政厅开始,奇怪的是,一个每个人都称之为“
- 一个典型路径是完全合格的方向是最简单,最直接的手段,从市政厅得到想要的房子
- 一个符号链接就像是一个空地用一记,让方向转发地址
- 将您带到这里的方向类型(无论是相对方向、完全限定方向,还是规范的完全限定方向)与此处是否通向房屋或任何带有转发方向的空地无关
- 有一个奇怪的例子,你方向的一条街道实际上是一个符号链接(绕道?一个门户?) - 如果我们看得太仔细,这个类比在这里就不成立了,所以让我们忽略它:-)
- 一个硬链接是由两个或多个不同的地址访问的房子。
- 想想榆树街和主街拐角处的房子。邮局错误地给了它两个地址:
10 Elm Str
和20 Main Str
。无论你去哪个地址,你最终都会在同一所房子里。 - 在我们陌生的小镇中,这些硬连接房屋可以有多个地址,而且地址不必彼此靠近。
- 不管你去哪个地址,都是同一个房子。它不是副本,也不是转发地址。神奇的是,一旦你进去,你最终会在同一所房子里,不管你以前从哪个地址到达那里。
- 引导您到房子的方向(无论使用哪个地址,或者方向是相对方向、完全限定方向,还是规范的完全限定方向)与该地址的房子是否是硬-链接房子与否
- 想想榆树街和主街拐角处的房子。邮局错误地给了它两个地址:
Addendum
附录
Edit
I asked someone who maintains Naming Files, Paths, and Namespaces page to let me know this. And he replied me.
Is this also Fully-qualified path? C:\directory..\directory\file.txt
编辑
我问过维护命名文件、路径和命名空间页面的人让我知道这一点。然后他回复了我。
这也是完全限定路径吗?C:\目录..\目录\文件.txt
I wonder what terms the maintainer of that page would use to differentiate between ..\file.txt
and C:\directory\..\directory\file.txt
since he calls them both relative path. I agree that double dots are a relative reference, but I wouldn't tag the whole path as relative because it has double dots in the middle of it. In his terminology, there doesn't seem to be a difference between fully-qualified and canonical. (Therein, I suppose, lies the source of your question).
我想知道该页面的维护者会使用哪些术语来区分它们..\file.txt
,C:\directory\..\directory\file.txt
因为他称它们为相对路径。我同意双点是相对引用,但我不会将整个路径标记为相对路径,因为它中间有双点。在他的术语中,完全限定和规范之间似乎没有区别。(我想,这就是你问题的来源)。
I come from a Unix and Java background, so perhaps that makes the difference. As I learned it:
我来自 Unix 和 Java 背景,所以也许这会有所不同。正如我所了解的:
relative/partially-qualified- location cannot be determined without the associated context providing information, e.g. the current working directory, the current drive, the drive's current directory, the shell PATH setting, the Java CLASSPATH setting, or the referencing URL.
absolute/fully-qualified- location is independent of the the associated context, i.e. the location is the same regardless of the current working directory, the current drive, the drive's current directory, the shell PATH setting, the Java CLASSPATH setting, or the referencing URL.
canonical- the simplest fully-qualified, i.e. no double-dots
相对/部分限定- 如果没有相关上下文提供信息,则无法确定位置,例如当前工作目录、当前驱动器、驱动器的当前目录、shell PATH 设置、Java CLASSPATH 设置或引用 URL。
绝对/完全限定- 位置独立于关联的上下文,即位置是相同的,无论当前工作目录、当前驱动器、驱动器的当前目录、shell PATH 设置、Java CLASSPATH 设置或引用网址。
canonical- 最简单的完全限定,即没有双点
So
所以
- ..\file.txt - relative
- C:\directory\..\directory\file.txt - fully-qualified
- C:\directory\file.txt - fully-qualified and canonical
- ..\file.txt -相对
- C:\directory\..\directory\file.txt -完全限定
- C:\directory\file.txt -完全限定和规范
That section of the MSDN pageisn't clear on C:\directory\..\directory\file.txt
: If C:\directory\..\directory\file.txt
is considered relative and won't work with Windows API that say they need a fully-qualified (but not necessarily canonical?) path, I'd suggest that page needs to make that clearer.
MSDN 页面的那部分不清楚C:\directory\..\directory\file.txt
:如果C:\directory\..\directory\file.txt
被认为是相对的并且不能与 Windows API 一起使用,说明他们需要一个完全合格的(但不一定是规范的?)路径,我建议该页面需要制作那个更清楚。
Fully-qualfied vs Relative
A file name is relativeto the current directory if it does not beginwith one of the following:
... * A disk designator with a backslash, for example "C:\" or "d:\"....
完全合格与相对
如果文件名不以以下之一开头,则文件名是相对于当前目录的:
... *带有反斜杠的磁盘指示符,例如“C:\”或“d:\”。...
Since C:\directory\..\directory\file.txt
starts with a disk designator with a blackslash, this path is fully-qualified, not relative.
由于C:\directory\..\directory\file.txt
以带黑斜杠的磁盘指示符开头,因此此路径是完全限定的,而不是相对的。
A path is also said to be relative if it contains "double-dots"; that is, two periods together in one component of the path. This special specifier is used to denote the directory above the current directory, otherwise known as the "parent directory". Examples of this format are as follows:
- "..\tmp.txt" specifies a file named tmp.txt located in the parent of the current directory.
- "....\tmp.txt" specifies a file that is two directories above the current directory.
- "..\tempdir\tmp.txt" specifies a file named tmp.txt located in a directory named tempdir that is a peer directory to the current directory.
如果路径包含“双点”,也称其为相对路径;也就是说,在路径的一个组成部分中同时存在两个时期。这个特殊的说明符用于表示当前目录之上的目录,也称为“父目录”。这种格式的例子如下:
- “..\tmp.txt”指定位于当前目录的父目录中的名为 tmp.txt 的文件。
- "..\tmp.txt" 指定了一个比当前目录高两个目录的文件。
- “..\tempdir\tmp.txt”指定位于名为 tempdir 的目录中的名为 tmp.txt 的文件,该目录是当前目录的对等目录。
I interpreted the phrase contains double dotsto mean leadingdouble dots. The examples show only leading double dots. The terminology "current directory" usually means process's current working directory or the drive's current directory, which has bearing only when talking about leading double dots. I can, however, see how the section could be interpreted the other way.
我理解这句话包含双点到平均领先的双点。示例仅显示前导双点。术语“当前目录”通常表示进程的当前工作目录或驱动器的当前目录,仅在谈论前导双点时才有意义。但是,我可以看到如何以另一种方式解释该部分。
Regardless, everyone grows up different and context is king, so I guess everyone will need to be careful of the nuances when reading docs or discussing with engineers of different backgrounds on what they mean by "fully-qualified" vs "relative"
无论如何,每个人的成长过程都不同,上下文为王,所以我想每个人在阅读文档或与不同背景的工程师讨论“完全合格”与“相对”的含义时都需要注意细微差别
回答by Pavel Radzivilovsky
No. IMHO, fully-qualified path only lets you find the resource in unambiguous manner (independent on the current working directory, PATH environ, etc) but two different paths may identify the same resource.
不。恕我直言,完全限定的路径只能让您以明确的方式(独立于当前工作目录、PATH 环境等)找到资源,但两个不同的路径可能会标识相同的资源。
I am unaware of a windows way of knowing if two paths point to the same file.
我不知道 Windows 知道两个路径是否指向同一个文件的方式。