获取创建文件的日期/时间 [英] Getting date/time of creation of a file

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

问题描述

这似乎是一个非常简单的问题,但我无法在任何地方找到确定的答案。如何通过Java的文件管理器获取创建文件的日期/时间?除了文件的名称,还有什么可以获取的文件的属性?

This seems like a pretty straightforward question but I haven't been able to find a definitive answer anywhere online. How can I get the date/time a file was created through Java's file manager? Aside from just the name of a file, what else can I get about the file's "properties"?

推荐答案

我不是确定如何使用Java 6及更低版本来获取它。使用Java 7的新文件系统API,它将如下所示:

I'm not sure how you'd get it using Java 6 and below. With Java 7's new file system APIs, it'd look like this:

Path path = ... // the path to the file
BasicFileAttributes attributes = 
    Files.readAttributes(path, BasicFileAttributes.class);
FileTime creationTime = attributes.creationTime();

正如CoolBeans所说,并不是所有的文件系统都存储创建时间。 BasicFileAttributes Javadoc 指出:

As CoolBeans said, not all file systems store the creation time. The BasicFileAttributes Javadoc states:


如果文件系统实现不支持时间戳来指示创建文件的时间,则此方法返回实现特定的默认值值,通常是最后修改的时间或代表时代的FileTime(1970-01-01T00:00:00Z)。

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).

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

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