在StackOverflow上还有一个类似的问题要问(如何在Java中获取文件的创建日期),但实际上并没有答案,因为OP具有可以通过其他机制解决的不同需求。我试图在目录中创建文件列表,该文件列表可以按年龄排序,因此需要文件创建日期。
经过大量的网络搜寻,我还没有找到任何好的方法来执行此操作。是否有获取文件创建日期的机制?
当前在Windows系统上的BTW可能也需要在Linux系统上工作。另外,我不能保证在名称中嵌入创建日期/时间的情况下,将遵循文件命名约定。
在StackOverflow上还有一个类似的问题要问(如何在Java中获取文件的创建日期),但实际上并没有答案,因为OP具有可以通过其他机制解决的不同需求。我试图在目录中创建文件列表,该文件列表可以按年龄排序,因此需要文件创建日期。
经过大量的网络搜寻,我还没有找到任何好的方法来执行此操作。是否有获取文件创建日期的机制?
当前在Windows系统上的BTW可能也需要在Linux系统上工作。另外,我不能保证在名称中嵌入创建日期/时间的情况下,将遵循文件命名约定。
Answers:
只要文件系统提供了Java nio,就可以选择访问creationTime和其他元数据。检查此链接了
例如(根据@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());readAttributes(file.toPath(), BasicFileAttributes.class)其他方法,您会得到:no suitable method found for readAttributes(File,Class<BasicFileAttributes>)     method Files.<A>readAttributes(Path,Class<A>,LinkOption...) is not applicable       (cannot infer type-variable(s) A         (argument mismatch; File cannot be converted to Path))
                    statxsyscall是在较新的Linux内核版本中添加的。
                    我已经使用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));
        }
    }
}作为此问题的后续部分-由于它专门与创建时间有关,并讨论了通过新的nio类获取它的时间-现在看来在JDK7的实现中您很不走运。附录:OpenJDK7中的行为相同。
在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);
    }
  }
}stat也不起作用。除非您运行的内核高于4.11,glibc高于2.28,coreutils高于8.31,否则stat它将报告文件的诞生。请参阅相关答案unix.stackexchange.com/questions/50177/birth-is-empty-on-ext4/…。目前,JDK不使用statx syscal。
                    这是有关如何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());
}该API java.io.File仅支持获取上次修改时间。互联网在此主题上也非常安静。
除非我错过了重要的事情,否则Java库(直到但尚未包括Java 7)都不包含此功能。因此,如果您不顾一切,一种解决方案是编写一些C(++)代码以调用系统例程并使用JNI对其进行调用。不过,大多数工作似乎已经在名为JNA的库中为您完成了。
但是,您可能仍需要为此用Java做一些特定于OS的编码,因为您可能找不到Windows和Unix / Linux / BSD / OS X中可用的相同系统调用。
File该类不具有此功能的原因是,大多数文件系统甚至都不跟踪此信息。那些并不总是同意何时更新它的人。
                    touch更改mod时间然后再次检查来更改它。
                    在Windows系统上,可以使用免费的FileTimes库。
将来,使用Java NIO.2(JDK 7)和java.nio.file.attribute软件包将更加容易。
但是请记住,大多数Linux文件系统不支持文件创建时间戳。
在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());
    }Instant.ofEpochMilli(date)。谢谢