Android:如何获取文件的创建日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2389225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: How to get a file's creation date?
提问by Jorgesys
This is my code:
这是我的代码:
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..?
}
}
回答by Jorgesys
The file creation date is not an available, but you can get the last-modified date:
文件创建日期不可用,但您可以获得最后修改日期:
File file = new File(filePath);
Date lastModDate = new Date(file.lastModified());
System.out.println("File last modified @ : "+ lastModDate.toString());
回答by CommonsWare
The file creation date is not an available piece of data exposed by the Java File
class. I recommend you rethink what you are doing and change your plan so you will not need it.
文件创建日期不是 JavaFile
类公开的可用数据。我建议你重新考虑你在做什么并改变你的计划,这样你就不需要它了。
回答by Lars Rye Jeppesen
Here's how I would do it
这是我将如何做到的
// 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();
}
}
回答by Steve Strates
Starting in API level 26, you can do this:
从 API 级别 26 开始,您可以执行以下操作:
File file = ...;
BasicFileAttributes attr = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
long createdAt = attr.creationTime().toMillis();
回答by coolcool1994
There is an alternate way. When you open the file for the first time save the lastModified date, before you modify the folder.
有一种替代方法。第一次打开文件时保存上次修改日期,然后再修改文件夹。
long createdDate =new File(filePath).lastModified();
And then when you close the file do
然后当你关闭文件时
File file =new File(filePath);
file.setLastModified(createdDate);
If you have done this since the file was created, then you will have the createdDate as the lastModified date all the time.
如果您在文件创建后就这样做了,那么您将始终将 createdDate 作为 lastModified 日期。
回答by Lukas
Having backward compatibility in mind I would rather use the following:
考虑到向后兼容性,我宁愿使用以下内容:
fun getLastModifiedTimeInMillis(file: File): Long? {
return try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
getLastModifiedTimeFromBasicFileAttrs(file)
} else {
file.lastModified()
}
} catch (x: Exception) {
x.printStackTrace()
null
}
}
@RequiresApi(Build.VERSION_CODES.O)
private fun getLastModifiedTimeFromBasicFileAttrs(file: File): Long {
val basicFileAttributes = Files.readAttributes(
file.toPath(),
BasicFileAttributes::class.java
)
return basicFileAttributes.creationTime().toMillis()
}
alternatively, if you are dealing with jpg, jpegs you can use ExifInterface
或者,如果您正在处理 jpg、jpegs,您可以使用ExifInterface