在Java中检测符号链接 [英] Detecting a symlink in Java

查看:149
本文介绍了在Java中检测符号链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个Java的文件对象,我该如何检测它是否引用符号链接?



(如果有帮助/重要,我知道文件指的是一个目录,而不是一个文件)

解决方案

File.getCanonicalPath() 解析符号链接


规范的路径名既是绝对路径,也是唯一的。规范形式的确切定义是依赖于系统的。如果需要,此方法首先将此路径名转换为绝对形式,就像调用getAbsolutePath()方法一样,然后以依赖于系统的方式将其映射到其唯一形式。这通常涉及删除多余的名称,如。和..,解析符号链接(在UNIX平台上),并将驱动器号转换为标准大小写(在Microsoft Windows平台上)。

我假设您可以比较 getCanonicalPath() getAbsolutePath()



更新:看来这个问题已经被问到了 - 检查答案


Given a Java 'File' object, how can I detect whether or not it refers to a symlink?

(If it helps/matters, I know the file refers to a directory, not to a file)

解决方案

File.getCanonicalPath() resolves symlinks

A canonical pathname is both absolute and unique. The precise definition of canonical form is system-dependent. This method first converts this pathname to absolute form if necessary, as if by invoking the getAbsolutePath() method, and then maps it to its unique form in a system-dependent way. This typically involves removing redundant names such as "." and ".." from the pathname, resolving symbolic links (on UNIX platforms), and converting drive letters to a standard case (on Microsoft Windows platforms).

I assume you can compare the result of getCanonicalPath() and getAbsolutePath().

Update: It appears this question has already been asked - check the answers there

这篇关于在Java中检测符号链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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