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:
我的Windows目录中有一组文件,这些文件是从其他地方复制的。在检查其中一个文件的属性(右键单击 - >属性)时,它显示:
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.
“Created”和“Accessed”字段基本上显示文件实际复制到新目录的时间,而“Modified”字段显示原始文件的修改日期。
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?
在Java上使用file.lastModified()我得到的是“访问”(或“创建”)时间戳。有没有办法获得原始文件的“修改”值?
3 个解决方案
#1
3
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.
查看这篇文章了解更多样本。
#2
2
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());
#3
0
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.
就JavaXT而言,Java 7并不适合你,如果你准备好只使用Windows平台,那么你可以尝试一些更奇特的方法。至于大多数* nix文件系统上都不存在文件创建属性,所以它似乎不是很大的限制。
1). pasre output of
1)。帕斯的输出
Runtime.getRuntime().exec("cmd /c dir c:\\logfile.log /tc");
working example here
这里的工作示例
2). Try another "external" library. E.g. FileTimes
2)。尝试另一个“外部”库。例如。 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 :-)
3)。您可以利用JNA直接调用Windows API函数。顺便说一句,当我试图找到带有JNA和文件属性函数的代码示例时,我发现了这个问题,所以你的问题似乎是重复的:-)
#1
3
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.
查看这篇文章了解更多样本。
#2
2
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());
#3
0
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.
就JavaXT而言,Java 7并不适合你,如果你准备好只使用Windows平台,那么你可以尝试一些更奇特的方法。至于大多数* nix文件系统上都不存在文件创建属性,所以它似乎不是很大的限制。
1). pasre output of
1)。帕斯的输出
Runtime.getRuntime().exec("cmd /c dir c:\\logfile.log /tc");
working example here
这里的工作示例
2). Try another "external" library. E.g. FileTimes
2)。尝试另一个“外部”库。例如。 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 :-)
3)。您可以利用JNA直接调用Windows API函数。顺便说一句,当我试图找到带有JNA和文件属性函数的代码示例时,我发现了这个问题,所以你的问题似乎是重复的:-)