Managing symbolic links - Java File Path IO

Java examples for File Path IO:Symbolic Link

Description

Managing symbolic links

Demo Code

import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {

  public static void main(String[] args) {
    Path path1 = null;/*from w  ww.  j  av a2  s.c om*/
    Path path2 = null;

    path1 = Paths.get("/home/docs/users.txt");
    path2 = Paths.get("/home/music/users.txt");

    System.out.println(Files.isSymbolicLink(path1));
    System.out.println(Files.isSymbolicLink(path2));

    try {
      Path path = Paths.get("C:/home/./music/users.txt");
      System.out.println("Normalized: " + path.normalize());
      System.out.println("Absolute path: " + path.toAbsolutePath());
      System.out.println("URI: " + path.toUri());
      System.out.println("toRealPath (Do not follow links):"
          + path.toRealPath(LinkOption.NOFOLLOW_LINKS));
      System.out.println("toRealPath: " + path.toRealPath());

      Path firstPath = Paths.get("/home/music/users.txt");
      Path secondPath = Paths.get("/docs/status.txt");
      System.out.println("From firstPath to secondPath: "
          + firstPath.relativize(secondPath));
      System.out.println("From secondPath to firstPath: "
          + secondPath.relativize(firstPath));
      System.out.println("exists (Do not follow links): "
          + Files.exists(firstPath, LinkOption.NOFOLLOW_LINKS));
      System.out.println("exists: " + Files.exists(firstPath));
      System.out.println("notExists (Do not follow links): "
          + Files.notExists(firstPath, LinkOption.NOFOLLOW_LINKS));
      System.out.println("notExists: " + Files.notExists(firstPath));

    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

}

Related Tutorials