Java 如何获得文件的正确文件创建日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21033928/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:05:30  来源:igfitidea点击:

How to get proper file creation date of file?

javafile

提问by Igor Kostenko

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.

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

Operation System: Windows 7 Java: SE-1.7

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

采纳答案by Eliza Weisman

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

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

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

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

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

Files.getAttribute(path, "basic:createdAt");将返回一个FileTime带有文件在支持BasicFileAttributeView. 您必须将其转换为java.util.Date对象,但我会让您自己解决。

Further Reading

进一步阅读

  • NIO APIfor getAttribute()
  • NIO APIfor BasicFileAttributeView
  • A tutorialfor using readAttributes()
  • A comprehensive tutorialon using FileAttributes
  • Another StackOverflow threadon the same topic
  • NIO APIgetAttribute()
  • NIO APIBasicFileAttributeView
  • 一个使用教程readAttributes()
  • 使用 FileAttributes 的综合教程
  • 同一主题的另一个 StackOverflow线程

回答by yshavit

You won't be able to do this across all systems, since not all systems even record that information. Linux doesn't, for instance; see this SO thread.

您将无法在所有系统上执行此操作,因为并非所有系统甚至都记录了该信息。例如,Linux 没有;看到这个 SO 线程

Many programs "modify" a file by copying it, making a change on the copy, and then moving the copy to the original file's location. So for those, there's not a meaningful distinction between creation and last-modification.

许多程序通过复制文件、对副本进行更改,然后将副本移动到原始文件的位置来“修改”文件。因此,对于那些,创建和最后修改之间没有有意义的区别。

回答by Jorgesys

How to get the creation date of a file in Java, using BasicFileAttributesclass, this is an example:

如何Java使用BasicFileAttributes类获取文件的创建日期,这是一个示例:

   Path path = Paths.get("C:\Users\jorgesys\workspaceJava\myfile.txt");
    BasicFileAttributes attr;
    try {
    attr = Files.readAttributes(path, BasicFileAttributes.class);

    System.out.println("Creation date: " + attr.creationTime());

    } catch (IOException e) {
    System.out.println("oops error! " + e.getMessage());
    }