I need to get file creation date in linux machine using java. Many workarounds worked good in windows but failed in linux. Need a way to get file creation time in linux. Please note that my linux machine has java6 installed. Any help is much appreciated. Thanks in adavance.
-
1mkyong.com/java/how-to-get-the-file-last-modified-date-in-java Even though the example is for Windows OS, surely it would work for linux...Ben Dale– Ben Dale2013-08-30 09:38:33 +00:00Commented Aug 30, 2013 at 9:38
-
possible duplicate of Determine file creation date in JavaRossC– RossC2013-08-30 09:39:59 +00:00Commented Aug 30, 2013 at 9:39
1 Answer
You can use stat command in Linux to get various date though creation date isn't available.
Instead you can get these 3 dates about a file:
- Time of last access
- Time of last modification (content of the file)
- Time of last change (metadata of file)
EDIT:
For getting creation/modification times of a file in Java (if using JDK 1.7) see: http://docs.oracle.com/javase/tutorial/essential/io/fileAttr.html
As per this document:
A word about time stamps: The set of basic attributes includes three time stamps: creationTime, lastModifiedTime, and lastAccessTime. Any of these time stamps might not be supported in a particular implementation, in which case the corresponding accessor method returns an implementation-specific value.
Unfortunately Linux/Unix doesn't store file's creation time hence you cannot get it.
PS: If you can use ext4 filesystem then you can get file's creation time in Unix/Linux.
2 Comments
A word about time stamps: The set of basic attributes includes three time stamps: creationTime, lastModifiedTime, and lastAccessTime. Any of these time stamps might not be supported in a particular implementation, in which case the corresponding accessor method returns an implementation-specific value. Which means if filesystem doesn't support it then it cannot get it. You can try though.