views:

211

answers:

1

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)

+3  A: 

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

Bozho
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)...
Matt Sheppard
Thanks - Using Apache's commons IO as noted in http://stackoverflow.com/questions/813710/java-1-6-determine-symbolic-links/813730#813730 seems like a good solution.
Matt Sheppard