简体   繁体   中英

“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?

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.

You could add this JavaXT library , and then you'll be able to do something like this:

javaxt.io.File file = new javaxt.io.File("/tmp/file.txt");
System.out.println("Created: " + file.getCreationTime());
System.out.println("Accessed: " + file.getLastAccessTime());
System.out.println("Modified: " + file.getLastModifiedTime());

As far as JavaXT, and Java 7 didn't work for you, you cat try some more exotic approaches, if you are ready to stick with Windows platform only. As far as, file creation attribute is not exists on most *nix file systems, so it's not seems like big restriction.

1). pasre output of

    Runtime.getRuntime().exec("cmd /c dir c:\\logfile.log /tc");

working example here

2). Try another "external" library. Eg FileTimes

3). You can utilize JNA to get time calling windows API functions directly. BTW, when I tried to found code example with JNA and file attributes functions, I've found this question , so your question seems to be a duplicate :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM