问题描述
有没有一种简单的方法来发现一个文件的Java创建时间? File类只有一个获取上次修改时间的方法。根据我在Google上发现的一些资源,File类没有提供getCreationTime()方法,因为并不是所有的文件系统都支持创建时间的想法。只有工作的解决方案,我发现了炮弹命令行,执行dir命令,看起来像输出文件的创建时间。我猜这是行得通的,我只需要支持Windows,但似乎很容易出错。
是否有任何第三方库提供我需要的信息? / p>
更新:最后,我认为不值得购买第三方库,但它们的API看起来很漂亮好,所以对于有这个问题的其他人来说这可能是一个不错的选择。
$ b
Path path = Paths.get(path / to / file);
BasicFileAttributes attributes = Files.readAttributes(path,BasicFileAttributes.class);
FileTime creationTime = attributes.creationTime();
请注意,并非所有操作系统都提供此信息。我相信在这些情况下,这将返回最后修改时间的mtime。
Windows确实提供了创建时间。
Is there an easy way to discover a File's creation time with Java? The File class only has a method to get the "last modified" time. According to some resources I found on Google, the File class doesn't provide a getCreationTime() method because not all file systems support the idea of a creation time.
The only working solution I found involes shelling out the the command line and executing the "dir" command, which looks like it outputs the file's creation time. I guess this works, I only need to support Windows, but it seems very error prone to me.
Are there any third party libraries that provide the info I need?
Update: In the end, I don't think it's worth it for me to buy the third party library, but their API does seem pretty good so it's probably a good choice for anyone else that has this problem.
With the release of Java 7 there is a built-in way to do this:
Path path = Paths.get("path/to/file");
BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class);
FileTime creationTime = attributes.creationTime();
It is important to note that not all operating systems provide this information. I believe in those instances this returns the mtime which is the last modified time.
Windows does provide creation time.
这篇关于如何用Java发现文件的创建时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!