Get the Last Access Time for a File

You will need to use the new file I/O API (NIO2) which comes with Java 7. It has a method lastAccessTime() for reading the last access time.

Here is a usage example:

Path file = ...
BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
FileTime time = attrs.lastAccessTime();

For more information see Managing Metadata in the Java Tutorial.


You can't do it with plain Java, you'll need to use JNI to access the platform specific data such as this or use extensions to the core Java library like the following:

javaxt.io.File file = new javaxt.io.File("path");
file.getLastAccessTime();

Or, if you have Java 7, go with Esko's answer and use NIO.