简体   繁体   English

java中的“上次访问”vs“最后修改”文件日期(windows)

[英]“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. 我的Windows目录中有一组文件,这些文件是从其他地方复制的。 On checking the properties of one of the files (right click -> Properties), it shows: 在检查其中一个文件的属性(右键单击 - >属性)时,它显示:

Created: Today, February 11, 2013, 2:51:56 PM 创建时间:今天,2013年2月11日,下午2:51:56

Modified: Tuesday, January 01, 2013, 8:30:04 AM 修改时间:2013年1月1日星期二,上午8:30:04

Accessed: Today, February 11, 2013, 2:51:56 PM 访问:今天,2013年2月11日,下午2:51:56

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. 在Java上使用file.lastModified()我得到的是“访问”(或“创建”)时间戳。 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 ). 除了在Java 7中使用“外部”库(如提到的JavaXT)之外,您还可以使用新的文件API(请参阅Java 7 nio.2教程 )。

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库 ,然后您将能够执行以下操作:

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. 至于JavaXT和Java 7不适合你,如果你准备坚持使用Windows平台,那么你可以尝试一些更奇特的方法。 As far as, file creation attribute is not exists on most *nix file systems, so it's not seems like big restriction. 至于大多数* nix文件系统上不存在文件创建属性,所以它似乎不是很大的限制。

1). 1)。 pasre output of 帕斯的输出

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

working example here 这里的工作范例

2). 2)。 Try another "external" library. 尝试另一个“外部”库。 Eg FileTimes 例如FileTimes

3). 3)。 You can utilize JNA to get time calling windows API functions directly. 您可以利用JNA直接调用Windows API函数。 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 :-) 顺便说一句,当我试图用JNA和文件属性函数找到代码示例时,我发现了这个问题 ,所以你的问题似乎是重复的:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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