在 Java 中确定文件创建日期 [英] Determine file creation date in Java

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

问题描述

在 StackOverflow 上还有另一个类似的问题要挖掘(如何在 Java 中获取文件的创建日期),但答案并不存在,因为 OP 有不同的需求,可以通过其他机制解决.我正在尝试在可以按年龄排序的目录中创建文件列表,因此需要文件创建日期.

There is another similar question to mine on StackOverflow (How to get creation date of a file in Java), but the answer isn't really there as the OP had a different need that could be solved via other mechanisms. I am trying to create a list of the files in a directory that can be sorted by age, hence the need for the file creation date.

经过大量网络搜索后,我还没有找到任何好的方法来做到这一点.是否有获取文件创建日期的机制?

I haven't located any good way to do this after much trawling of the web. Is there a mechanism for getting file creation dates?

顺便说一句,目前在 Windows 系统上,可能也需要它在 Linux 系统上工作.另外,我不能保证在名称中嵌入了创建日期/时间的地方会遵循文件命名约定.

BTW, currently on a Windows system, may need this to work on a Linux system as well. Also, I can't guarantee that a file naming convention would be followed where the creation date/time is embedded in the name.

推荐答案

Java nio 可以选择访问 creationTime 和其他元数据,只要文件系统提供它.检查这个链接

Java nio has options to access creationTime and other meta-data as long as the filesystem provides it. Check this link out

例如(根据 @ydaetskcoR 的评论提供):

For example (provided based on @ydaetskcoR's comment):

Path file = ...;
BasicFileAttributes attr = Files.readAttributes(file.toPath(), BasicFileAttributes.class);

System.out.println("creationTime: " + attr.creationTime());
System.out.println("lastAccessTime: " + attr.lastAccessTime());
System.out.println("lastModifiedTime: " + attr.lastModifiedTime());

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

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