Java Is Symbolic Link isSymbolicLink(File file)

Here you can find the source of isSymbolicLink(File file)

Description

Determines if a file is a symbolic link.

License

Open Source License

Parameter

Parameter Description
file the file to determine

Exception

Parameter Description
IOException if the file could not be read.

Return

true ifthe file is a symbolic links; false otherwise.

Declaration

public static boolean isSymbolicLink(File file) throws IOException 

Method Source Code


//package com.java2s;
import java.io.File;
import java.io.IOException;

public class Main {
    /**/* w w  w .j a  va2s  .  co m*/
     * Determines if a file is a symbolic link.
     * 
     * @param file
     *            the file to determine
     * @return true ifthe file is a symbolic links; false otherwise.
     * @throws IOException
     *             if the file could not be read.
     */
    public static boolean isSymbolicLink(File file) throws IOException {
        if (file == null) {
            throw new NullPointerException("File must not be null");
        }
        File fileInCanonicalDir = null;
        if (file.getParent() == null) {
            fileInCanonicalDir = file;
        } else {
            File canonicalDir = file.getParentFile().getCanonicalFile();
            fileInCanonicalDir = new File(canonicalDir, file.getName());
        }

        return !fileInCanonicalDir.getCanonicalFile().equals(fileInCanonicalDir.getAbsoluteFile());
    }
}

Related

  1. isSymbolicLink(File file)
  2. isSymbolicLink(File file)
  3. isSymbolicLink(File file)
  4. isSymbolicLink(File parent, String name)