Подтвердить что ты не робот

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

Ответ 1

Дата создания файла недоступна, но вы можете получить дата последнего изменения:

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

Ответ 2

Дата создания файла не является доступной частью данных, открытой классом Java File. Я рекомендую вам переосмыслить, что вы делаете, и изменить свой план, чтобы он вам не понадобился.

Ответ 3

Вот как бы я это сделал

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

Ответ 4

Существует альтернативный способ. Когда вы впервые открываете файл, сохраните последнюю дату с модификацией, прежде чем изменять папку.

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

И затем, когда вы закроете файл, сделайте

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

Если вы это сделали, так как файл был создан, вы будете иметь созданную дату как lastModified date все время.