java如何获取文件的创建时间呢?

戚薇 Java经验 发布时间:2022-07-13 22:58:00 阅读数:12394 1
下文笔者将使用java代码获取文件的创建时间的方法分享,如下所示
由于java中没有获取文件创建时间的api
但我们可以通过获取属性的方式获取文件的创建时间
例:
 
private Long getFileCreateTime(String filePath){
    File file = new File(filePath);
    try {
        Path path= Paths.get(filePath);
        BasicFileAttributeView basicview= Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS );
        BasicFileAttributes attr = basicview.readAttributes();
        return attr.creationTime().toMillis();
    } catch (Exception e) {
        e.printStackTrace();
        return file.lastModified();
    }
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202207/16577243323974.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者