canonicalpath和absolutepath之间的区别是什么? [英] what's the difference between canonicalpath and absolutepath?

查看:641
本文介绍了canonicalpath和absolutepath之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Java中的getPath(),getAbsolutePath()和getCanonicalPath()有什么区别?

这两者之间有什么区别?

Any difference between those two?

canonicalpath和absolutepath?

canonicalpath and absolutepath?

如果有差异,将需要一个真实世界的例子。

If having difference, a real world example will be needed.

推荐答案

区别在于文件 [1] 只有一条规范路径,而文件的绝对路径可能很多(取决于系统)。例如,在Unix系统上, / usr / local /../ bin / usr / bin 。 getCanonicalPath()解决这些歧义并返回(唯一的)规范路径。因此,如果当前目录是 / usr / local ,那么:

The difference is that there is only one canonical path to a file[1], while there can be many absolute paths to a file (depending on the system). For instance, on a Unix system, /usr/local/../bin is the same as /usr/bin. getCanonicalPath() resolves those ambiguities and returns the (unique) canonical path. So if the current directory was /usr/local, then:

File file = new File("../bin");
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
System.out.println(file.getCanonicalPath());

将打印:


../ bin

/usr/local/../bin

/ usr / bin

../bin
/usr/local/../bin
/usr/bin

Per Voo的建议:在Unix系统上,如果符号链接存在, getCanonicalPath()也将解析符号链接。硬链接被视为普通文件(基本上就是它们)。但请注意,这些方法不需要存在文件才能成功。

Per Voo's suggestion: on Unix systems, getCanonicalPath() will also resolve symbolic links if the symbolic link exists. Hard links are treated like normal files (which is basically what they are). Note, however, that a file need not exist for these methods to succeed.

[1] 嗯,不完全。正如@Tom Hale在评论中指出的那样,如果文件系统支持硬链接目录,则可能存在指向给定文件的多个规范路径。

这篇关于canonicalpath和absolutepath之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆