Java Is Link File isLink(final File file)

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

Description

Checks if the given File Object may be a symbolic link.
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.

License

Open Source License

Parameter

Parameter Description
file a parameter

Return

true if the file is a symbolic link or does not even exist. false if not

Declaration

public static boolean isLink(final File file) 

Method Source Code

//package com.java2s;
/* *********************************************************************** *
 * project: org.matsim.*//from  w  w  w  .  j a v  a 2 s . c  o  m
 * IOUtils.java
 *                                                                         *
 * *********************************************************************** *
 *                                                                         *
 * copyright       : (C) 2007 by the members listed in the COPYING,        *
 *                   LICENSE and WARRANTY file.                            *
 * email           : info at matsim dot org                                *
 *                                                                         *
 * *********************************************************************** *
 *                                                                         *
 *   This program 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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *   See also COPYING, LICENSE and WARRANTY file                           *
 *                                                                         *
 * *********************************************************************** */

import java.io.File;

import java.io.IOException;

public class Main {
    /**
     * Checks if the given File Object may be a symbolic link.<br />
     *
     * 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.<br />
     *
     * 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?).<br />
     *
     * 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.<br />
     *
     * Note that this method has problems if the file is specified with a relative
     * path like "./" or "../".<br />
     *
     * @see <a href="http://www.idiom.com/~zilla/Xfiles/javasymlinks.html">Javasymlinks on www.idiom.com</a>
     *
     * @param file
     * @return true if the file is a symbolic link or does not even exist. false if not
     *
     * @author dgrether
     */
    public static boolean isLink(final File file) {
        try {
            if (!file.exists()) {
                return true;
            }
            String cnnpath = file.getCanonicalPath();
            String abspath = file.getAbsolutePath();
            return !abspath.equals(cnnpath);
        } catch (IOException ex) {
            System.err.println(ex);
            return true;
        }
    }
}

Related

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