Setting time-related attributes of a file or directory - Java File Path IO

Java examples for File Path IO:Directory

Description

Setting time-related attributes of a file or directory

Demo Code

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.util.Calendar;

public class Main {
  public static void main(String[] args) throws Exception {
    Path path = Paths.get("C:/home/docs/users.txt");
    BasicFileAttributeView view = Files.getFileAttributeView(path,
        BasicFileAttributeView.class);
    FileTime lastModifedTime;/*from   w ww  . j a  v  a  2  s .c om*/
    FileTime lastAccessTime;
    FileTime createTime;

    BasicFileAttributes attributes = view.readAttributes();
    lastModifedTime = attributes.lastModifiedTime();
    createTime = attributes.creationTime();

    long currentTime = Calendar.getInstance().getTimeInMillis();
    lastAccessTime = FileTime.fromMillis(currentTime);

    view.setTimes(lastModifedTime, lastAccessTime, createTime);
    System.out.println(attributes.lastAccessTime());
  }
}

Related Tutorials