Created
May 8, 2020 17:54
-
-
Save varundwarkani/27061120e8db4d6db71f3ba4b0690fae to your computer and use it in GitHub Desktop.
checkAndDeleteBackupFile() method is used to delete the oldest backup file and keep the count of file to maximum of MAXIMUM_DATABASE_FILE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void checkAndDeleteBackupFile(File directory, String path) { | |
//This is to prevent deleting extra file being deleted which is mentioned in previous comment lines. | |
File currentDateFile = new File(path); | |
int fileIndex = -1; | |
long lastModifiedTime = System.currentTimeMillis(); | |
if (!currentDateFile.exists()) { | |
File[] files = directory.listFiles(); | |
if (files != null && files.length >= MAXIMUM_DATABASE_FILE) { | |
for (int i = 0; i < files.length; i++) { | |
File file = files[i]; | |
long fileLastModifiedTime = file.lastModified(); | |
if (fileLastModifiedTime < lastModifiedTime) { | |
lastModifiedTime = fileLastModifiedTime; | |
fileIndex = i; | |
} | |
} | |
if (fileIndex != -1) { | |
File deletingFile = files[fileIndex]; | |
if (deletingFile.exists()) { | |
deletingFile.delete(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment