代码之家  ›  专栏  ›  技术社区  ›  Alex

安卓:如何获取文件的创建日期?

  •  59
  • Alex  · 技术社区  · 16 年前

    这是我的代码:

    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..?
        }
    }
    
    6 回复  |  直到 8 年前
        1
  •  156
  •   Jorgesys    9 年前

    文件 创建日期不可用 但是你可以得到 last-modified date :

    File file = new File(filePath);
    Date lastModDate = new Date(file.lastModified());
    Log.i("File last modified @ : "+ lastModDate.toString());
    
        2
  •  22
  •   LA_    11 年前

    我可以这样做

    // 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();
       }
    }
    
        3
  •  20
  •   CommonsWare    16 年前

    文件创建日期不是由Java公开的可用数据片段。 File 班级。我建议你重新考虑一下你在做什么,改变你的计划,这样你就不需要它了。

        4
  •  5
  •   coolcool1994    13 年前

    有另一种方法。 第一次打开文件时,请在修改文件夹之前保存上次修改的日期。

    long createdDate =new File(filePath).lastModified();
    

    当你关闭文件的时候

    File file =new File(filePath);
    file.setLastModified(createdDate);
    

    如果您在创建文件之后就这样做了,那么您将一直将createdDate作为最后修改的日期。

        5
  •  3
  •   Steve Strates    8 年前

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

    File file = ...;
    BasicFileAttributes attr = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
    long createdAt = attr.creationTime().toMillis();
    
        6
  •  0
  •   LarsH    8 年前

    虽然在Android平台API中可能没有任何方法可以做到这一点,但在某些情况下,有 kludges->strong>可以工作 。这里有一个:启动一个“http://Dealth.Android /App/App/UIutixHealm命令%28 Java.Lang.Stand % 29”Re=“NoFoLoLoNeFror”> shell Hyf=“http://Dealth.Android /Cudio/Java/Lang/RunTime.html yEXEC(Java.Lang.Stule[])”Re=“NoFoLoLoNeFror”>命令执行

    ls-gnc文件名
    < /代码> 
    
    

    根据ls--usage>code>的输出,-c选项告诉ls“use ctime for timestamps”。 在我的Galaxy J3(Android 7)上,它产生

    -rwxrwx--x 1 1015 7623004 2018-03-28 10:54文件名
    < /代码> 
    
    

    从中可以提取日期和时间。不幸的是,您无法控制日期格式,并且没有关于用于显示的时区的信息。但总比什么都没有好。

    免责声明:这不适用于其他一些Android设备。例如,在运行android 4.2.2的华为H30上,lsdoes't take the-c.option.更糟的是:在HTC Desire 550(Android 7.0)上,ls->command声明采用-c选项来显示ctime,如果运行ls-cl->code>您将得到一个没有错误的日期输出。但显示的日期实际上是最后一次修改时间,而不是创建时间!因此,不仅不支持创建时间,而且很难发现它不受支持。

    E例. 这是一个:启动一个shell命令待办事项

    ls -gnc filename
    

    根据产量ls --usage,的-c选项告诉ls“将ctime用于时间戳”。 在我的Galaxy J3(Android 7)上,它产生

    -rwxrwx--x 1 1015 7623004 2018-03-28 10:54 filename
    

    从中可以提取日期和时间。不幸的是,您无法控制日期格式,并且没有关于用于显示的时区的信息。但总比什么都没有好。

    免责声明:这不适用于其他一些Android设备。例如,在运行Android 4.2.2的华为H30上,ls不接受-C选择权。更糟的是:在HTC Desire550(Android 7.0)上,LS命令声明使用-c选项来显示ctime,如果运行ls -cl您将得到一个没有错误的日期输出。但显示的日期实际上是最后一次修改时间,而不是创建时间!因此,不仅不支持创建时间,而且很难发现它不受支持。