Links/Aliases/Shortcuts in Java

j.p.lewis
www.idiom.com/~zilla

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:

  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.