java 获取文件的最后访问时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/920259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the Last Access Time for a File
提问by Veera
I know that using Fileobject we can get the last modified time for a File(i.e. File.lastModified()). But, my requirement is to get the last accessed timefor a Filein Java. How do I get it?
我知道使用Fileobject 我们可以获得 a File(即File.lastModified())的最后修改时间。但是,我的要求是获取Java 中a的最后访问时间File。我如何得到它?
回答by Esko Luontola
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.
您将需要使用Java 7 附带的新文件 I/O API (NIO2)。它有一个方法lastAccessTime()用于读取上次访问时间。
Here is a usage example:
这是一个使用示例:
Path file = ...
BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
FileTime time = attrs.lastAccessTime();
For more information see Managing Metadatain the Java Tutorial.
有关更多信息,请参阅Java 教程中的管理元数据。
回答by Kris
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 librarylike the following:
你不能用普通的 Java 来做,你需要使用 JNI 来访问平台特定的数据,比如这个,或者使用核心 Java 库的扩展,如下所示:
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.
或者,如果您有 Java 7,请使用 Esko 的答案并使用 NIO。

