66 android
这是我的代码:
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..?
}
}
Run Code Online (Sandbox Code Playgroud)
Jor*_*sys 174
文件创建日期不可用,但您可以获取上次修改日期:
File file = new File(filePath);
Date lastModDate = new Date(file.lastModified());
Log.i("File last modified @ : "+ lastModDate.toString());
Run Code Online (Sandbox Code Playgroud)
小智 22
我就是这样做的
// 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();
}
}
Run Code Online (Sandbox Code Playgroud)
Com*_*are 21
文件创建日期不是Java File类公开的可用数据.我建议你重新考虑你在做什么,改变你的计划,这样你就不需要了.
从API级别26开始,您可以执行以下操作:
File file = ...;
BasicFileAttributes attr = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
long createdAt = attr.creationTime().toMillis();
Run Code Online (Sandbox Code Playgroud)
还有另一种方式.在您修改文件夹之前,首次打开文件时保存lastModified日期.
long createdDate =new File(filePath).lastModified();
Run Code Online (Sandbox Code Playgroud)
然后当你关闭文件时
File file =new File(filePath);
file.setLastModified(createdDate);
Run Code Online (Sandbox Code Playgroud)
如果您在创建文件后已完成此操作,那么您将始终将createdDate作为lastModified日期.
| 归档时间: |
|
| 查看次数: |
69668 次 |
| 最近记录: |