Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"last accessed" vs "last modified" file date in java (windows)

I have a set of files on my windows directory which are copied from elsewhere. On checking the properties of one of the files (right click -> Properties), it shows:

Created: Today, February 11, 2013, 2:51:56 PM

Modified: Tuesday, January 01, 2013, 8:30:04 AM

Accessed: Today, February 11, 2013, 2:51:56 PM

The "Created" and "Accessed" fields basically show the time that the file was actually copied to the new directory, while the "Modified" field shows the modified date of the original file.

In Java on using file.lastModified() what I get back is the "Accessed" (or "Created") timestamp. Is there a way to get the "Modified" value of the original file?

like image 521
tajji Avatar asked Jan 24 '26 15:01

tajji


1 Answers

Along with utilising "external" library (like mentioned JavaXT) in Java 7 you can also use new file API (check out this Java 7 nio.2 tutorial).

File attribFile = new File("/tmp/file.txt");
Path attribPath = attribFile.toPath();
BasicFileAttributeView basicView =
    attribPath.getFileAttributeView(BasicFileAttributeView.class);
BasicFileAttributes basicAttribs = basicView.readAttributes();

System.out.println("Created: " + basicAttribs.creationTime());
System.out.println("Accessed: " + basicAttribs.lastAccessTime());
System.out.println("Modified: " + basicAttribs.lastModifiedTime());

Check out this article for extra samples.

like image 152
Recontemplator Avatar answered Jan 26 '26 05:01

Recontemplator



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!