Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to discover a File's creation time with Java?

Tags:

Is there an easy way to discover a File's creation time with Java? The File class only has a method to get the "last modified" time. According to some resources I found on Google, the File class doesn't provide a getCreationTime() method because not all file systems support the idea of a creation time.

The only working solution I found involes shelling out the the command line and executing the "dir" command, which looks like it outputs the file's creation time. I guess this works, I only need to support Windows, but it seems very error prone to me.

Are there any third party libraries that provide the info I need?

Update: In the end, I don't think it's worth it for me to buy the third party library, but their API does seem pretty good so it's probably a good choice for anyone else that has this problem.

like image 438
Outlaw Programmer Avatar asked Aug 28 '08 15:08

Outlaw Programmer


People also ask

How can you find the creation date of a file?

Windows file properties You can also see the modified date by viewing the file properties. Right-click the file and select Properties. In the Properties window, the Created date, Modified date, and Accessed date is displayed, similar to the example below.

Where is the file created in Java?

If you start your Java app in a batch file, and doubleclick on the link to it, the current directory will be the directory where the batch file resided, but this can be changed in the link.


1 Answers

With the release of Java 7 there is a built-in way to do this:

Path path = Paths.get("path/to/file"); BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class); FileTime creationTime = attributes.creationTime(); 

It is important to note that not all operating systems provide this information. I believe in those instances this returns the mtime which is the last modified time.

Windows does provide creation time.

like image 155
ajon Avatar answered Sep 26 '22 03:09

ajon