如何获取文件的正确文件创建日期? [英] How to get proper file creation date of file?

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

问题描述

我不需要上次修改时间而不是上次文件访问时间,但文件创建时间。我还没有找到有关此信息。也许是一些库?

I need not last modification time and not last file accessed time, but file creation time. I have not found information about this. Maybe some libs?

Path p = Paths.get(f.getAbsoluteFile().toURI());
BasicFileAttributes view = null;
try {
    view = Files.getFileAttributeView(p,
                            BasicFileAttributeView.class).readAttributes();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
FileTime creationTime = view.creationTime();

此代码创建时间无效,今天返回日期。

In this code creation time is not valid and return today date.

操作系统:Windows 7
Java:SE-1.7

Operation System: Windows 7 Java: SE-1.7

推荐答案

正如yshavit所说,并非所有操作系统都记录创建的日期。但是,您应该能够使用 java.nio.file 来确定具有此功能的操作系统上的此信息 - 请参阅 files.getAttribute - 请注意 BasicFileAttributeView 有一个字段对于 creationTime

As yshavit said, not all operating systems record the date created. However, you should be able to use java.nio.file to determine this information on operating systems that do have this functionality - see the documentation for files.getAttribute - note that BasicFileAttributeView has a field for creationTime.

您可以使用 FileSystems.getDefault(); 确定当前操作系统支持 FileAttributeView

You can use FileSystems.getDefault(); to determine what FileAttributeViews are supported on the current operating system.

Files.getAttribute(path,basic:createdAt); 将返回一个 FileTime 对象,其中包含在支持的系统上创建文件的日期 BasicFileAttributeView 。你必须将它转换为 java.util.Date 对象,但我会让你自己解决这个问题。

Files.getAttribute(path, "basic:createdAt"); will return a FileTime object with the date the file was created on a system that supports BasicFileAttributeView. You'll have to convert it to a java.util.Date object, but I'll let you figure that out on your own.

  • NIO API for getAttribute()
  • NIO API for BasicFileAttributeView
  • A tutorial for using readAttributes()
  • A comprehensive tutorial on using FileAttributes
  • Another StackOverflow thread on the same topic

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

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