Get the Last Access Time for a File

久未见 提交于 2019-11-27 08:54:37

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!