Determine file creation date in Java

Todd picture Todd · Apr 27, 2010 · Viewed 173.8k times · Source

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?

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.

Answer

ring bearer picture ring bearer · Apr 27, 2010

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

For example(Provided based on @ydaetskcoR's comment):

Path file = ...;
BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);

System.out.println("creationTime: " + attr.creationTime());
System.out.println("lastAccessTime: " + attr.lastAccessTime());
System.out.println("lastModifiedTime: " + attr.lastModifiedTime());