Android:如何获取文件的创builddate?

这是我的代码:

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..? } } 

文件创builddate不可用 ,但您可以获取上次修改的date :

 File file = new File(filePath); Date lastModDate = new Date(file.lastModified()); Log.i("File last modified @ : "+ lastModDate.toString()); 

文件创builddate不是Java File类公开的可用数据片段。 我build议你重新考虑你在做什么,改变你的计划,这样你就不需要它了。

这是我将如何做到这一点

 // 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(); } } 

有一个替代方法。 当您第一次打开该文件保存lastModifieddate,在修改文件夹之前。

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

然后当你closures文件做

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

如果自从创build文件之后完成此操作,那么您将始终将createdDate作为lastModifieddate。