How to check file is a Symbolic Link Using Java

Many time we need to check that a file is symbolic link or actual file using Java.  Here is one simple function from apache commons io library to check symbolic link.

static boolean isWindows() {
return File.separatorChar == \'\\\';
}
public static boolean isSymlink(File file) throws IOException {
if (file == null) {
throw new NullPointerException("File must not be null");
}
if (isWindows()) {
return false;
}
File fileInCanonicalDir = null;
if (file.getParent() == null) {
fileInCanonicalDir = file;
} else {
File canonicalDir = file.getParentFile().getCanonicalFile();
fileInCanonicalDir = new File(canonicalDir, file.getName());
}
if (fileInCanonicalDir.getCanonicalFile().equals(fileInCanonicalDir.getAbsoluteFile())) {
return false;
} else {
return true;
}
}

You can also use apache-commons-io FileUtils class isSymlink function directly if you wan to use that library.