Read the lastModifiedTime and lastAccessTime attributes of the target and sets them to the link. - Java File Path IO

Java examples for File Path IO:Symbolic Link

Description

Read the lastModifiedTime and lastAccessTime attributes of the target and sets them to the link.

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) {
    Path link = FileSystems.getDefault().getPath("test");
    Path target = FileSystems.getDefault().getPath("C:/folder1/photos",
        "test.jpg");

    try {//from   w  ww  . ja  va 2  s  . c  om
      Files.createSymbolicLink(link, target);

      FileTime lm = (FileTime) Files.getAttribute(target,
          "basic:lastModifiedTime", LinkOption.NOFOLLOW_LINKS);
      FileTime la = (FileTime) Files.getAttribute(target,
          "basic:lastAccessTime", LinkOption.NOFOLLOW_LINKS);
      Files.setAttribute(link, "basic:lastModifiedTime", lm,
          LinkOption.NOFOLLOW_LINKS);
      Files.setAttribute(link, "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