Java Is Symbolic Link isSymlink(File file)

Here you can find the source of isSymlink(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

private static boolean isSymlink(File file) throws IOException 

Method Source Code


//package com.java2s;
/*/*from   w  ww  .ja  v  a2  s. co m*/
 * 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.
 *
 * (modified version)
 */

import java.io.*;

public class Main {
    /**
     * The system separator character.
     */
    private static final char SYSTEM_SEPARATOR = File.separatorChar;
    /**
     * The Windows separator character.
     */
    private static final char WINDOWS_SEPARATOR = '\\';

    /**
     * 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.
     *
     * @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 Commons IO 2.0
     */
    private static boolean isSymlink(File file) throws IOException {
        if (file == null) {
            throw new NullPointerException("File must not be null");
        }
        if (isSystemWindows()) {
            return false;
        }
        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());
    }

    /**
     * Determines if Windows file system is in use.
     *
     * @return true if the system is Windows
     */
    private static boolean isSystemWindows() {
        return SYSTEM_SEPARATOR == WINDOWS_SEPARATOR;
    }
}

Related

  1. isSymbolicLink(File parent, String name)
  2. isSymlink(File base, File curious)
  3. isSymLink(File file)
  4. isSymlink(File file)
  5. isSymlink(File file)
  6. isSymlink(File file)
  7. isSymlink(File file)
  8. isSymlink(File file)
  9. isSymlink(File file)