Creating a Hard Link - Java File Path IO

Java examples for File Path IO:Symbolic Link

Introduction

The following code creates a hard 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",
        "test.jpg");
    try {//from w  ww  .ja v a2  s  .c  om
      Files.createLink(link, target);
      System.out.println("The link was successfully created!");
    } catch (IOException | UnsupportedOperationException | SecurityException e) {
      if (e instanceof SecurityException) {
        System.err.println("Permission denied!");
      }
      if (e instanceof UnsupportedOperationException) {
        System.err.println("An unsupported operation was detected!");
      }
      if (e instanceof IOException) {
        System.err.println("An I/O error occured!");
      }
      System.err.println(e);
    }
  }
}

Result


Related Tutorials