使用jdk1.7获取java文件的最后访问时间的示例 [英] An example to get last access time of file in java using jdk1.7

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

问题描述

朋友请帮忙。我知道使用jdk1.7我们可以得到文件的最后访问时间。任何人都可以提供一个代码示例来获取文件的最后访问时间吗?

Friends please help. I know that using jdk1.7 we can get the last access time of file. Can anyone give an example with codes to get last access time of file?

推荐答案

由于您在问题中提到 jdk1.7 ,你应该真正研究方法 lastAccessTime()。我不确定你的真实问题是什么,但如果你的意思是想要一个使用jdk7读取文件上次访问时间的代码示例,请看下面的内容。

Since you mentioned in your question using jdk1.7 , you should really look into the interface BasicFileAttributes on method lastAccessTime() . I'm not sure what is your real question but if you mean you want an example with codes on reading a file last access time using jdk7, take a look at below.

import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.Files;

/** 
 * compile using jdk1.7
 *
 */
public class ReadFileLastAccess {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception
    {
        Path file_dir = Paths.get("/home/user/");
        Path file = file_dir.resolve("testfile.txt");
        BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);       
        System.out.println("Last accessed at:" + attrs.lastAccessTime());

    }

}

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

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