Close

Java IO & NIO - Files.isSymbolicLink() Examples

Java IO & NIO Java Java API 


Class:

java.nio.file.Files

java.lang.Objectjava.lang.Objectjava.nio.file.Filesjava.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




See Also