在 Java 中确定文件创建日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2723838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine file creation date in Java
提问by Todd
There is another similar question to mine on StackOverflow (How to get creation date of a file in Java), but the answer isn't really there as the OP had a different need that could be solved via other mechanisms. I am trying to create a list of the files in a directory that can be sorted by age, hence the need for the file creation date.
在 StackOverflow 上还有另一个类似的问题(如何在 Java 中获取文件的创建日期),但答案并不存在,因为 OP 有不同的需求,可以通过其他机制解决。我正在尝试在可以按年龄排序的目录中创建文件列表,因此需要文件创建日期。
I haven't located any good way to do this after much trawling of the web. Is there a mechanism for getting file creation dates?
在对网络进行了大量搜索之后,我还没有找到任何好的方法来做到这一点。是否有获取文件创建日期的机制?
BTW, currently on a Windows system, may need this to work on a Linux system as well. Also, I can't guarantee that a file naming convention would be followed where the creation date/time is embedded in the name.
顺便说一句,目前在 Windows 系统上,可能也需要它在 Linux 系统上工作。另外,我不能保证在名称中嵌入了创建日期/时间的地方会遵循文件命名约定。
回答by Carl Smotricz
The API of java.io.File
only supports getting the last modifiedtime. And the Internet is very quiet on this topic as well.
的 APIjava.io.File
仅支持获取上次修改时间。互联网在这个话题上也很安静。
Unless I missed something significant, the Java library as is (up to but not yet including Java 7) does not include this capability. So if you were desperate for this, one solution would be to write some C(++) code to call system routines and call it using JNI. Most of this work seems to be already done for you in a library called JNA, though.
除非我遗漏了一些重要的东西,否则 Java 库(直到但尚未包括 Java 7)不包括此功能。因此,如果您对此感到绝望,一种解决方案是编写一些 C(++) 代码来调用系统例程并使用 JNI 调用它。不过,大部分工作似乎已经在名为JNA的库中为您完成。
You may still need to do a little OS specific coding in Java for this, though, as you'll probably not find the same system calls available in Windows and Unix/Linux/BSD/OS X.
不过,您可能仍需要为此用 Java 进行一些特定于操作系统的编码,因为您可能找不到在 Windows 和 Unix/Linux/BSD/OS X 中可用的相同系统调用。
回答by ring bearer
Java niohas options to access creationTime and other meta-data as long as the filesystem provides it. Check this linkout
只要文件系统提供,Java nio就可以选择访问 creationTime 和其他元数据。检查此链接了
For example(Provided based on @ydaetskcoR's comment):
例如(根据@ydaetskcoR 的评论提供):
Path file = ...;
BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);
System.out.println("creationTime: " + attr.creationTime());
System.out.println("lastAccessTime: " + attr.lastAccessTime());
System.out.println("lastModifiedTime: " + attr.lastModifiedTime());
回答by Jacek S
On a Windows system, you can use free FileTimeslibrary.
在 Windows 系统上,您可以使用免费的FileTimes库。
This will be easier in the future with Java NIO.2 (JDK 7) and the java.nio.file.attribute package.
将来使用Java NIO.2 (JDK 7) 和 java.nio.file.attribute 包会更容易。
But remember that most Linux filesystems don't support file creation timestamps.
但请记住,大多数 Linux 文件系统不支持文件创建时间戳。
回答by David Nugent
As a follow-up to this question - since it relates specifically to creation time and discusses obtaining it via the new nio classes - it seems right now in JDK7's implementation you're out of luck. Addendum: same behaviour is in OpenJDK7.
作为这个问题的后续 - 因为它特别与创建时间有关并讨论了通过新的 nio 类获取它 - 现在似乎在 JDK7 的实现中你不走运。附录:相同的行为在 OpenJDK7 中。
On Unix filesystems you cannot retrieve the creation timestamp, you simply get a copy of the last modification time. So sad, but unfortunately true. I'm not sure why that is but the code specifically does that as the following will demonstrate.
在 Unix 文件系统上,您无法检索创建时间戳,您只需获取上次修改时间的副本。好难过,可惜是真的。我不知道为什么会这样,但代码专门这样做,如下所示。
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.*;
public class TestFA {
static void getAttributes(String pathStr) throws IOException {
Path p = Paths.get(pathStr);
BasicFileAttributes view
= Files.getFileAttributeView(p, BasicFileAttributeView.class)
.readAttributes();
System.out.println(view.creationTime()+" is the same as "+view.lastModifiedTime());
}
public static void main(String[] args) throws IOException {
for (String s : args) {
getAttributes(s);
}
}
}
回答by Oleksandr Tarasenko
I've solved this problem using JDK 7 with this code:
我已经使用带有以下代码的 JDK 7 解决了这个问题:
package FileCreationDate;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class Main
{
public static void main(String[] args) {
File file = new File("c:\1.txt");
Path filePath = file.toPath();
BasicFileAttributes attributes = null;
try
{
attributes =
Files.readAttributes(filePath, BasicFileAttributes.class);
}
catch (IOException exception)
{
System.out.println("Exception handled when trying to get file " +
"attributes: " + exception.getMessage());
}
long milliseconds = attributes.creationTime().to(TimeUnit.MILLISECONDS);
if((milliseconds > Long.MIN_VALUE) && (milliseconds < Long.MAX_VALUE))
{
Date creationDate =
new Date(attributes.creationTime().to(TimeUnit.MILLISECONDS));
System.out.println("File " + filePath.toString() + " created " +
creationDate.getDate() + "/" +
(creationDate.getMonth() + 1) + "/" +
(creationDate.getYear() + 1900));
}
}
}
回答by Jorgesys
This is a basic example of how to get the creation date of a file in Java
, using BasicFileAttributes
class:
这是如何Java
使用BasicFileAttributes
类获取文件的创建日期的基本示例:
Path path = Paths.get("C:\Users\jorgesys\workspaceJava\myfile.txt");
BasicFileAttributes attr;
try {
attr = Files.readAttributes(path, BasicFileAttributes.class);
System.out.println("Creation date: " + attr.creationTime());
//System.out.println("Last access date: " + attr.lastAccessTime());
//System.out.println("Last modified date: " + attr.lastModifiedTime());
} catch (IOException e) {
System.out.println("oops error! " + e.getMessage());
}
回答by zhangxinqiang
in java1.7+ You can use this code to get file`s create time !
在 java1.7+ 中,您可以使用此代码获取文件的创建时间!
private static LocalDateTime getCreateTime(File file) throws IOException {
Path path = Paths.get(file.getPath());
BasicFileAttributeView basicfile = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
BasicFileAttributes attr = basicfile.readAttributes();
long date = attr.creationTime().toMillis();
Instant instant = Instant.ofEpochMilli(date);
return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
}