Detecting a symlink in Java [duplicate]
Asked Answered
G

2

23

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)

Grindelia answered 22/3, 2010 at 6:28 Comment(0)
T
29

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'.

Tabescent answered 26/10, 2013 at 20:57 Comment(3)
There's a caveat - like Files.exists, Files.isSymbolicLink can sometimes return a false negative.Thomsen
@Thomsen could you provide an example?Chandos
@Chandos if an exception occurs when checking for the information, the method will swallow the exception and return false, regardless of whether the file is a symbolic link. Because of that, in many cases, for Files.exists(), what you often really want is !Files.notExists(). But it wasn't the case 100% of the time either. This mess caused us to ban usage of the methods in our codebase, redirecting calls to our own versions which throw the exception.Thomsen
W
36

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

Walter answered 22/3, 2010 at 6:37 Comment(2)
Sounds like it might work - I wonder how expensive it is to call on a real file-system object (I assume it's having to check each directory up the tree, where as I only care about the last one)...Grindelia
Thanks - Using Apache's commons IO as noted in #814210 seems like a good solution.Grindelia
T
29

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'.

Tabescent answered 26/10, 2013 at 20:57 Comment(3)
There's a caveat - like Files.exists, Files.isSymbolicLink can sometimes return a false negative.Thomsen
@Thomsen could you provide an example?Chandos
@Chandos if an exception occurs when checking for the information, the method will swallow the exception and return false, regardless of whether the file is a symbolic link. Because of that, in many cases, for Files.exists(), what you often really want is !Files.notExists(). But it wasn't the case 100% of the time either. This mess caused us to ban usage of the methods in our codebase, redirecting calls to our own versions which throw the exception.Thomsen

© 2022 - 2024 — McMap. All rights reserved.