Check if a path is a symbolic link - Java File Path IO

Java examples for File Path IO:Symbolic Link

Description

Check if a path is a symbolic link

Demo Code

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

public class Main {
  public static void main(String[] args) {

    Path link = FileSystems.getDefault().getPath("test");
    Path target = FileSystems.getDefault().getPath("C:/folder1/photos",
        "my_winner.jpg");

    try {//from w  w  w  .j a  va  2  s  .c om
      Boolean link_isSymbolicLink_2 = (Boolean) Files.getAttribute(link,
          "basic:isSymbolicLink");
      Boolean target_isSymbolicLink_2 = (Boolean) Files.getAttribute(target,
          "basic:isSymbolicLink");

      System.out.println(link.toString() + " is a symbolic link ? "
          + link_isSymbolicLink_2);
      System.out.println(target.toString() + " is a symbolic link ? "
          + target_isSymbolicLink_2);
    } catch (IOException | UnsupportedOperationException e) {
      System.err.println(e);
    }
  }
}

Result


Related Tutorials