Detecting a symlink in Java [duplicate]

六眼飞鱼酱① 提交于 2019-11-27 22:47:47

Also you can use isSymbolicLink(Path path) method. It will be more reliable.

java.io.File file = ...;
boolean isSymbolicLink = Files.isSymbolicLink(file.toPath());

Similar examples from Java Doc 'Detecting a Symbolic Link'.

Bozho

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!