Create a symbolic link with the same lastModifiedTime and lastAccessTime as the target - Java File Path IO

Java examples for File Path IO:Symbolic Link

Description

Create a symbolic link with the same lastModifiedTime and lastAccessTime as the target

Demo Code

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;

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

    // create a symbolic link with the same lastModifiedTime and lastAccessTime
    // as the target
    Path link3 = FileSystems.getDefault().getPath("test");
    Path target3 = FileSystems.getDefault().getPath("C:/folder1/photos",
        "test.jpg");
    try {/*from   w w w  .  j  a v a 2s .  c  o m*/
      Files.createSymbolicLink(link3, target3);

      FileTime lm = (FileTime) Files.getAttribute(target3,
          "basic:lastModifiedTime", LinkOption.NOFOLLOW_LINKS);
      FileTime la = (FileTime) Files.getAttribute(target3,
          "basic:lastAccessTime", LinkOption.NOFOLLOW_LINKS);
      Files.setAttribute(link3, "basic:lastModifiedTime", lm, LinkOption.NOFOLLOW_LINKS);
      Files.setAttribute(link3, "basic:lastAccessTime", la, LinkOption.NOFOLLOW_LINKS);
    } catch (IOException | UnsupportedOperationException | SecurityException e) {
      if (e instanceof SecurityException) {
        System.err.println("Permision 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