java 获取创建文件的日期/时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6885269/
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
Getting date/time of creation of a file
提问by Brian
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 的文件管理器获取文件创建的日期/时间?除了文件名之外,我还能获得有关文件“属性”的信息吗?
回答by ColinD
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:
我不确定您如何使用 Java 6 及以下版本获得它。使用 Java 7 的新文件系统 API,它看起来像这样:
Path path = ... // the path to the file
BasicFileAttributes attributes =
Files.readAttributes(path, BasicFileAttributes.class);
FileTime creationTime = attributes.creationTime();
As CoolBeans said, not all file systems store the creation time. The BasicFileAttributes Javadocstates:
正如 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).
如果文件系统实现不支持时间戳来指示创建文件的时间,则此方法返回实现特定的默认值,通常是上次修改时间或表示纪元的 FileTime (1970-01-01T00: 00:00Z)。