Java Is Symbolic Link isSymlink(File file)

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

Description

is Symlink

License

Open Source License

Declaration

public static boolean isSymlink(File file) throws IOException 

Method Source Code


//package com.java2s;
/*//  w  ww . j  a  v a  2  s.co m
 * Hello Minecraft!.
 * Copyright (C) 2013  huangyuhui <huanghongxun2008@126.com>
 *
 * 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program.  If not, see {http://www.gnu.org/licenses/}.
 */

import java.io.File;

import java.io.IOException;

import java.util.Objects;

public class Main {
    public static boolean isSymlink(File file) throws IOException {
        Objects.requireNonNull(file, "File must not be null");
        if (File.separatorChar == '\\')
            return false;
        File fileInCanonicalDir;
        if (file.getParent() == null)
            fileInCanonicalDir = file;
        else {
            File canonicalDir = file.getParentFile().getCanonicalFile();
            fileInCanonicalDir = new File(canonicalDir, file.getName());
        }

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

    public static String getName(String filename) {
        if (filename == null)
            return null;
        int index = indexOfLastSeparator(filename);
        return filename.substring(index + 1);
    }

    public static int indexOfLastSeparator(String filename) {
        if (filename == null)
            return -1;
        int lastUnixPos = filename.lastIndexOf(47);
        int lastWindowsPos = filename.lastIndexOf(92);
        return Math.max(lastUnixPos, lastWindowsPos);
    }
}

Related

  1. isSymLink(File file)
  2. isSymlink(File file)
  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 symlinkFile)