Some applications need to distinguish between a "real" file and a link/shortcut/alias to a file. Java.io.File does not have an isLink() function, but it appears that links can be distinguished in pure java in at least some cases.
Here's what I understand of the link/alias/shortcut behavior on different operating systems:
For a link that actually points to something (either a file or a directory), the absolute path is the path through the link, whereas the canonical path is the path the link references.
Dangling links appear as files of size zero, and generate a FileNotFoundException when you try to open them. Unfortunately non-existent files have the same behavior (size zero - why did't file.length() throw an exception if the file doesn't exist, rather than returning length zero?).
The routine below appears to detect Unix/Linux symbolic links, but it also returns true for non-existent files. Note that if a directory is a link, all the contents of this directory will appear as symbolic links as well.
public static boolean isLink(File file) { try { if (!file.exists()) return true; else { String cnnpath = file.getCanonicalPath(); String abspath = file.getAbsolutePath(); return !abspath.equals(cnnpath); } } catch(IOException ex) { System.err.println(ex); return true; } } //isLink
Update 2006 Tom O'Neill at google looked into this matter and found that there is still no complete solution to this problem in java.