Java Is Link File isLink(File file)

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

Description

Return true if the specified file is a link
Be careful, it does work in almost case, but not all

License

Open Source License

Exception

Parameter Description
IOException an exception

Declaration

public static boolean isLink(File file) throws IOException 

Method Source Code

//package com.java2s;
/*/*from   w w  w.j  ava 2s.c  om*/
 * Copyright 2010, 2011 Institut Pasteur.
 * 
 * This file is part of ICY.
 * 
 * ICY is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ICY is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ICY. If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import java.io.IOException;

public class Main {
    public static boolean isLink(String path) throws IOException {
        return isLink(new File(getGenericPath(path)));
    }

    /**
     * Return true if the specified file is a link<br>
     * Be careful, it does work in almost case, but not all
     * 
     * @throws IOException
     */
    public static boolean isLink(File file) throws IOException {
        if (file == null)
            return false;

        final File canon;

        if (file.getParent() == null)
            canon = file;
        else
            canon = new File(file.getParentFile().getCanonicalFile(), file.getName());

        // we want to ignore case whatever system does
        return !canon.getCanonicalFile().getAbsolutePath().equalsIgnoreCase(canon.getAbsolutePath());
    }

    public static String getGenericPath(String path) {
        if (path != null)
            return path.replace('\\', '/');

        return null;
    }
}

Related

  1. isLink(@Nonnull File file)
  2. isLink(File file)
  3. isLink(File file)
  4. isLink(final File file)
  5. isLink(java.io.File f)