Java File Link isSymlink(final File file)

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

Description

Determines whether the specified file is a Symbolic Link rather than an actual file.

License

Apache License

Parameter

Parameter Description
file the file to check

Exception

Parameter Description
IOException if an IO error occurs while checking the file

Return

true if the file is a Symbolic Link

Declaration

public static boolean isSymlink(final File file) throws IOException 

Method Source Code


//package com.java2s;
/*/*from ww  w  .j  a va2s .  c o m*/
 * Written using code from the Apache commons-io project. To the code from
 * the commons project applies that the following:
 * 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;

import java.io.IOException;

public class Main {
    /**
     * Determines whether the specified file is a Symbolic Link rather than an actual file.
     * <p>
     * Will not return true if there is a Symbolic Link anywhere in the path, only if the specific file is.
     * <p>
     * <b>Note:</b> the current implementation always returns {@code false} if the system is detected as Windows using
     * {@link FilenameUtils#isSystemWindows()}
     * <p>
     * For code that runs on Java 1.7 or later, use the following method instead:
     * <br> {@code boolean java.nio.file.Files.isSymbolicLink(Path path)}
     *
     * @param file the file to check
     * @return true if the file is a Symbolic Link
     * @throws IOException if an IO error occurs while checking the file
     * @since 2.0
     */
    public static boolean isSymlink(final File file) throws IOException {
        if (file == null) {
            throw new NullPointerException("File must not be null");
        }
        if (File.separatorChar == '\\') {
            return false;
        }
        File fileInCanonicalDir = null;
        if (file.getParent() == null) {
            fileInCanonicalDir = file;
        } else {
            final File canonicalDir = file.getParentFile().getCanonicalFile();
            fileInCanonicalDir = new File(canonicalDir, file.getName());
        }

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

Related

  1. isLink(File f)
  2. isSymbolicLink(File file)
  3. isSymbolicLink(String file)
  4. isSymlink(File file)
  5. isSymLink(File file)
  6. isSymlinkJava7(@Nonnull File file)
  7. makeSymLink(File target, File destination)
  8. readSymbolicLink(@Nonnull File symlink)
  9. readSymlinkTarget(File file)