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

I have a set of files in my windows directory that are copied from other sources. When checking the properties of one of the files (right click -> Properties) it shows:

Created: Today, Feb 11, 2013, 2:51:56 pm

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

Available: Today, Feb 11, 2013 at 2:51:56 PM

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

In Java, when used file.lastModified()

, what I get is a label "Access" (or "Created"). Is there a way to get the "Modified" value of the original file?

+3


source to share


3 answers


Along with using an "external" library (like the JavaXT mentioned) in Java 7, you can also use the 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 this article for more samples.

+3


source


You can add this JavaXT library and then you can 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());

      

+2


source


As far as JavaXT and Java 7 are concerned, it didn't work for you, you can try more exotic approaches if you're willing to stick with just the Windows platform. Since the file create attribute doesn't exist on most * nix filesystems, so it doesn't seem like a big limitation.

1). pasre output

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

      

working example here

2). Try another "external" library. For example. FileTimes

3). You can use JNA to call Windows API functions directly. BTW, when I tried to find some sample code with JNA functions and file attributes, I found this question , so your question seems to be duplicate :-)

0


source







All Articles