Close

Java IO & NIO - Files.getLastModifiedTime() Examples

Java IO & NIO Java Java API 


Class:

java.nio.file.Files

java.lang.Objectjava.lang.Objectjava.nio.file.Filesjava.nio.file.FilesLogicBig

Method:

public static FileTime getLastModifiedTime(Path path,
                                           LinkOption... options)
                                    throws IOException

Returns a file's last modified time. The options array may be used to indicate how symbolic links are handled for the case that the file is a symbolic link. By default, symbolic links are followed and the file attribute of the final target of the link is read. If the option LinkOption#NOFOLLOW_LINKS is present then symbolic links are not followed.

Parameters:
path - the path to the file
options - options indicating how symbolic links are handled
Returns:
a FileTime representing the time the file was last modified, or an implementation specific default when a time stamp to indicate the time of last modification is not supported by the file system


Examples


package com.logicbig.example.files;

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

public class GetLastModifiedTimeExample {

public static void main(String... args) throws IOException {
Path tempFile = Files.createTempFile("test-file", ".txt");
FileTime lastModifiedTime = Files.getLastModifiedTime(tempFile);
System.out.println(lastModifiedTime);
}
}

Output

2017-12-11T03:57:15.462634Z




This code must be run will Admin privilege. See Files.createSymbolicLink() example for more details.

package com.logicbig.example.files;

import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;

public class GetLastModifiedTimeExample2 {

public static void main(String... args) throws Exception {
Path tempDirectory = Files.createTempDirectory("test-dir");
Path path = Files.createTempFile(tempDirectory, "test-file", ".txt");
System.out.println("src last modified: " + Files.getLastModifiedTime(path));
//creating a symbolic link
Path symbolicLink = Files.createSymbolicLink(path.getParent().resolve("sym-linked-file"), path);
System.out.println("sym last modified: " + Files.getLastModifiedTime(symbolicLink));

//pause for a while
Thread.sleep(1000);

//modify source
System.out.println("-- modifying the original file --");
Files.write(path, "test content".getBytes());

//get last modified again
System.out.println("src last modified: " + Files.getLastModifiedTime(path));
System.out.println("sym last modified: " + Files.getLastModifiedTime(symbolicLink));
System.out.println("sym last modified with NOFOLLOW_LINKS option: " +
Files.getLastModifiedTime(symbolicLink, LinkOption.NOFOLLOW_LINKS));

}
}

Output

src last modified: 2017-12-11T04:35:39.033711Z
sym last modified: 2017-12-11T04:35:39.033711Z
-- modifying the original file --
src last modified: 2017-12-11T04:35:40.04793Z
sym last modified: 2017-12-11T04:35:40.04793Z
sym last modified with NOFOLLOW_LINKS option: 2017-12-11T04:35:39.038717Z




See Also