首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android:如何获取文件的创建日期?

Android:如何获取文件的创建日期?
EN

Stack Overflow用户
提问于 2010-03-06 03:07:16
回答 5查看 87.8K关注 0票数 81

这是我的代码:

代码语言:javascript
复制
File TempFiles = new File(Tempfilepath);
if (TempFiles.exists()) {
    String[] child = TempFiles.list();
    for (int i = 0; i < child.length; i++) {
        Log.i("File: " + child[i] + " creation date ?");
        // how to get file creation date..?
    }
}
EN

回答 5

Stack Overflow用户

发布于 2010-09-16 16:53:48

下面是我会怎么做

代码语言:javascript
复制
// Used to examplify deletion of files more than 1 month old
// Note the L that tells the compiler to interpret the number as a Long
final int MAXFILEAGE = 2678400000L; // 1 month in milliseconds

// Get file handle to the directory. In this case the application files dir
File dir = new File(getFilesDir().toString());

// Obtain list of files in the directory. 
// listFiles() returns a list of File objects to each file found.
File[] files = dir.listFiles();

// Loop through all files
for (File f : files ) {

   // Get the last modified date. Milliseconds since 1970
   Long lastmodified = f.lastModified();

   // Do stuff here to deal with the file.. 
   // For instance delete files older than 1 month
   if(lastmodified+MAXFILEAGE<System.currentTimeMillis()) {
      f.delete();
   }
}
票数 24
EN

Stack Overflow用户

发布于 2010-03-06 03:55:20

文件创建日期不是Java File类公开的可用数据。我建议你重新考虑你正在做的事情,改变你的计划,这样你就不需要它了。

票数 23
EN

Stack Overflow用户

发布于 2018-01-25 04:42:21

从API级别26开始,您可以执行以下操作:

代码语言:javascript
复制
File file = ...;
BasicFileAttributes attr = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
long createdAt = attr.creationTime().toMillis();
票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2389225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档