获取文件的最后访问时间 [英] Get the Last Access Time for a File

查看:143
本文介绍了获取文件的最后访问时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道使用 File 对象,我们可以得到最后修改的时间为$ code> File (即 File.lastModified())。但是,我的要求是在Java中获取最后访问时间的文件。如何获取?

解决方案

您将需要使用 Java 7附带的新文件I / O API(NIO2)。它具有一种方法 lastAccessTime(),用于阅读上一次访问时间。



以下是使用示例:

 路径文件= ... 
BasicFileAttributes attrs = Files.readAttributes(file,BasicFileAttributes.class);
FileTime time = attrs.lastAccessTime();

有关更多信息,请参阅管理元数据在Java教程中。


I know that using File object we can get the last modified time for a File (i.e. File.lastModified()). But, my requirement is to get the last accessed time for a File in Java. How do I get it?

解决方案

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.

这篇关于获取文件的最后访问时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆