views:

282

answers:

3

Hello,

There is another similar question to mine on StackOverflow (How to get creation date of a file in Java), but the answer isn't really there as the OP had a different need that could be solved via other mechanisms. I am trying to create a list of the files in a directory that can be sorted by age, hence the need for the file creation date.

I haven't located any good way to do this after much trawling of the web. Is there a mechanism for getting file creation dates?

Thanks, Todd

BTW, currently on a Windows system, may need this to work on a Linux system as well. Also, I can't guarantee that a file naming convention would be followed where the creation date/time is embedded in the name.

A: 

The API of java.io.File only supports getting the last modified time. And the Internet is very quiet on this topic as well.

Unless I missed something significant, the Java library as is (up to but not yet including Java 7) does not include this capability. So if you were desperate for this, one solution would be to write some C(++) code to call system routines and call it using JNI. Most of this work seems to be already done for you in a library called JNA, though.

You may still need to do a little OS specific coding in Java for this, though, as you'll probably not find the same system calls available in Windows and Unix/Linux/BSD/OS X.

Carl Smotricz
Yeah, Java 7 would be great as the nio appears to have this in basic attributes. Never thought I would complain about being born too early! ;)
Todd
The reason the `File` class doesn't have this capability is that most filesystems don't even track this information. And those that do don't always agree on when it should be updated.
Syntactic
@syntactic, very good point.
ring bearer
+2  A: 

Java nio has options to access creationTime and other meta-data as long as the filesystem provides it. Check this link out

ring bearer
Looks like way to go
Romain Hippeau
This would be best, but it is Java 7. We are still using 6, but I will investigate our upgrade options.
Todd
+2  A: 

On a Windows system, you can use free FileTimes library.

This will be easier in the future with Java NIO.2 (JDK 7) and the java.nio.file.attribute package.

But remember that most Linux filesystems don't support file creation timestamps.

Jacek S