Java IO & NIO Java Java API 

Previous MethodNext Method

Class:

java.nio.file.Files

java.lang.Objectjava.nio.file.FilesLogicBig

Method:

public static boolean isSymbolicLink(Path path)

Tests whether a file is a symbolic link.

Parameters:

path - The path to the file

Returns:

true if the file is a symbolic link; false if the file does not exist, is not a symbolic link, or it cannot be determined if the file is a symbolic link or not.

Examples

package com.logicbig.example.files;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class IsSymbolicLinkExample {

    public static void main(String... args) throws IOException {
        Path path = Files.createTempFile("test-file", ".txt");
        boolean symbolicLink = Files.isSymbolicLink(path);
        System.out.println(symbolicLink);
    }
}

Output

false

package com.logicbig.example.files;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class IsSymbolicLinkExample2 {

    public static void main(String... args) throws IOException {
        Path dir = Files.createTempDirectory("test-dir");
        Path path = Files.createTempFile(dir, "test-file", ".txt");
        //this will fail if not run with admin privileges
        Path symPath = Files.createSymbolicLink(dir.resolve("sym-" + path.getFileName()), path);
        boolean symbolicLink = Files.isSymbolicLink(symPath);
        System.out.println("symbolic link: " + symbolicLink);
    }
}

Output

symbolic link: true
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐