How to get proper file creation date of file?

后端 未结 3 1920
感动是毒
感动是毒 2021-02-15 16:45

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 = Pat         


        
3条回答
  •  南旧
    南旧 (楼主)
    2021-02-15 17:18

    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.

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

    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.

    Further Reading

    • 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

提交回复
热议问题