Get file last modified time

File meta data is stored within the file system and directories. It tracks information about each of the file which is typically referred to as file attributes. File attributes include size of a file, is it a directory, a symbolic link, a regular file, determine if the file is hidden, retrieve the last modified time and find when a file was created.

Setup

private static final String SOURCE = "com/levelup/java/io/get-last-modified-time.txt";

Path source;

@Before
public void setUp() throws IOException, URISyntaxException {

    source = Paths.get(this.getClass().getClassLoader().getResource(SOURCE)
            .toURI());
}

Straight up Java

File.lastModified

@Test
public void file_last_modified_java () {

    File file = source.toFile();

    long lastModified = file.lastModified();

    assertEquals(1389664624000l, lastModified);

}

Java 7 File I/O

@Test
public void file_last_modified_nio() throws IOException {

    BasicFileAttributes attr = Files.readAttributes(source,
            BasicFileAttributes.class);

    FileTime lastModified = attr.lastModifiedTime();

    assertEquals(1389664624000l, lastModified.toMillis());
}