Honey Honey - 4 months ago 6
Java Question

Retain latest n files based on purge time

I have tried with how to retain files based on n number of days. This is the logic to do so.

public static void purgeFiles(int daysToRetain, String directory){

File backupDir = new File(directory);
long purgeTime = System.currentTimeMillis() - (daysToRetain * 24L * 60L * 60L * 1000L);
if(backupDir.exists()){
File[] listOfBackupFiles = backupDir.listFiles();
for(File backupFile : listOfBackupFiles){
if(backupFile.lastModified() < purgeTime){
if(backupFile.isFile()){
System.out.println(backupFile.getName()+" is going to delete");
backupFile.delete();
}
}

}

}

}


Here I have a doubt that how to retain latest 10 files in a directory. File creation date and times are different.

There is no fixed time to create files. Files will not get create daily or weekly or monthly. One file will get create in 2 days, others will be in 5 days.

So, I want to retain latest 10 files.

Answer

Best way to list files in Java, sorted by Date Modified?

You can use a Comparator to order your array by date modified. Then, just check if the array is bigger then 10, and execute your code for all items after 10.

Example:

import java.io.File;
    import java.util.Arrays;
    import java.util.Comparator;

    public class PurgeFiles
    {

        public static void main(String[] args)
        {
            purgeFiles(10, 10, "C:/testdir/");
        }

        public static void purgeFiles(int daysToRetain, int filesToRetain, String directory)
        {

            File backupDir = new File(directory);
            long purgeTime = System.currentTimeMillis() - (daysToRetain * 24L * 60L * 60L * 1000L);
            if (backupDir.exists())
            {
                File[] listOfBackupFiles = backupDir.listFiles();

                Arrays.sort(listOfBackupFiles, new Comparator<File>()
                {
                    public int compare(File f1, File f2)
                    {
                        return Long.valueOf(f2.lastModified()).compareTo(f1.lastModified());
                    }
                });

                if(listOfBackupFiles.length > filesToRetain){
                    File[] listOfBackupFilesToCheckForDeletion = new File[listOfBackupFiles.length - filesToRetain];
                    System.arraycopy(listOfBackupFiles, filesToRetain, listOfBackupFilesToCheckForDeletion, 0, listOfBackupFiles.length - filesToRetain);

                    for (File backupFile : listOfBackupFilesToCheckForDeletion)
                    {
                        if (backupFile.lastModified() < purgeTime)
                        {
                            if (backupFile.isFile())
                            {
                                System.out.println(backupFile.getName() + ", " + backupFile.lastModified() + " is going to delete");
                                backupFile.delete();
                            }
                        }
                    }
                }
            }
        }
    }