Updating the file's last modified time can also be accomplished with the Files.setLastModifiedTime() method: - Java File Path IO

Java examples for File Path IO:File Attribute

Description

Updating the file's last modified time can also be accomplished with the Files.setLastModifiedTime() method:

Demo Code

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

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

    Path path = Paths//from   w w w  .j  a  va2 s .  c  o  m
        .get(
            "D:\\folder0\\Java\\JDK7\\folder5\\code\\folder1\\folder2\\folder6",
            "test.txt");

    // update any or all of the file's last modified time, last access time, and
    // create time attributes
    long time = System.currentTimeMillis();
    FileTime fileTime = FileTime.fromMillis(time);
    // update the file's last modified time with the setLastModifiedTime method
    try {
      Files.setLastModifiedTime(path, fileTime);
    } catch (IOException e) {
      System.err.println(e);
    }

  }
}

Result


Related Tutorials