加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 获取文件创建的日期/时间

发布时间:2020-12-14 05:38:08 所属栏目:Java 来源:网络整理
导读:这似乎是一个非常直截了当的问题,但我无法在网上找到明确的答案.如何通过 Java的文件管理器获取文件的创建日期/时间?除了文件的名称,我还能得到关于文件的“属性”的其他内容吗? 解决方法 我不确定你是如何使用Java 6及以下版本的.使用Java 7的新文件系统A
这似乎是一个非常直截了当的问题,但我无法在网上找到明确的答案.如何通过 Java的文件管理器获取文件的创建日期/时间?除了文件的名称,我还能得到关于文件的“属性”的其他内容吗?

解决方法

我不确定你是如何使用Java 6及以下版本的.使用Java 7的新文件系统API,它看起来像这样:
Path path = ... // the path to the file
BasicFileAttributes attributes = 
    Files.readAttributes(path,BasicFileAttributes.class);
FileTime creationTime = attributes.creationTime();

正如CoolBeans所说,并非所有文件系统都存储创建时间. BasicFileAttributes Javadoc声明:

If the file system implementation does not support a time stamp to indicate the time when the file was created then this method returns an implementation specific default value,typically the last-modified-time or a FileTime representing the epoch (1970-01-01T00:00:00Z).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读