Java - File Input Output Symbolic Links

Introduction

Operations on a symbolic link are transparent to the application.

When an operation is performed on a symbolic link, the operating system performs the operation on the target of the link.

The delete, move, and rename operations are performed directly on the link, rather than on its target.

It is possible to have a circular reference in a symbolic link, where the target of a symbolic link points back to the original link.

Java can detect a circular reference in a symbolic link.

You can work with symbolic links using the java.nio.file.Files class.

You can use its isSymbolicLink(Path p) method to check if the file denoted by the specified path is a symbolic link.

The createSymbolicLink() method of the Files class creates a symbolic link.

The createSymbolicLink() not be supported on all platforms.

Demo

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

public class Main {
  public static void main(String[] args) {
    Path existingFilePath = Paths.get("C:\\myData\\Main.java");
    Path symLinkPath = Paths.get("C:\\test1_link.txt");
    try {//from   ww w  .j  a  v  a 2  s.co m
      Files.createSymbolicLink(symLinkPath, existingFilePath);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Result

Java follows the symbolic link by default.

You can specify whether you want to follow a symbolic link or not.

To not to follow a symbolic link, use the enum constant LinkOption.NOFOLLOW_LINKS.

The LinkOption enum is declared in the java.nio.file package.