简体   繁体   中英

Best way to get file date created from Java on a UNIX system?

java.io.File doesn't provide a way to get a file's creation date. You can get file.lastModified, but not anything like dateCreated.

Java 7 adds the excellent java.nio.file package with access to date created, but it's not out yet.

My question: what's the best way to get a file's date created from Java on a UNIX/OSX system? I suppose it's executing a shell script, but my command line skills are pretty weak. So if shell scripting's the way to go, if you could provide a full example I'd be very grateful.

Thanks!

Unix doesn't have a "creation date". The only dates stored on files are modification date, which stores the time the file contents where changed; the access date, which stores the last time a file was read; and the "change date", which stores the last time the file's metadata was changed. (The metadata contains things like permission bits, ownership, etc.)

If you review the structure supported by the stat(2) API, you can see the three timespec's.

From the command line, the easiest way to get the creation date is with mdls . I think you'd want to do /usr/bin/mdls -name kMDItemFSCreationDate $filename (where $filename is the file you're asking about). You can specify multiple filenames, but that might make it harder to parse the output.

你不能 - 它不存储在任何地方

It actually depends on the file system, and most file systems don't provide such feature as storing a file "birth time". You need to check the filesystem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM