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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With