Getting a List of Old Files in a Directory in Java by Comparing the Files Creation Time


We can browse a list of files using Java‘s listFiles method in File Object, and then we can iterate them one by one and get the file creation Time of each – and compare with the current timestamp. If it is older than a threshold, we can append the file path into a list.

Here is the Java’s file utility function that allows us to browser a folder and get a list of files that are older (file creation time) than a threshold.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.helloacm;
 
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.sql.Timestamp;
import java.util.*;
import java.io.*;
 
public class FileUtils {
    List<File> getFilesOlder(Path folder, int secondsOld) {
        var file = new File(folder.toAbsolutePath().toString());
        var files = file.listFiles();
        var oldFiles = new ArrayList<File>();
        if (files == null) {
            return oldFiles;
        }
        for (var f: files) {
            BasicFileAttributes fat = null;
            try {
                fat = Files.readAttributes(Path.of(f.getAbsolutePath()),
                        BasicFileAttributes.class);
                var creationTime = fat.creationTime().toMillis();
                var timestamp = new Timestamp(System.currentTimeMillis());
                var now = timestamp.getTime();
                if (creationTime <= now - secondsOld * 1000) {
                    oldFiles.add(f);
                }
            } catch (IOException e) {
                //  ignore errors when file creation time cannot be retrieved
                e.printStackTrace();
            }
        }
        return oldFiles;
    }
}
package com.helloacm;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.sql.Timestamp;
import java.util.*;
import java.io.*;

public class FileUtils {
    List<File> getFilesOlder(Path folder, int secondsOld) {
        var file = new File(folder.toAbsolutePath().toString());
        var files = file.listFiles();
        var oldFiles = new ArrayList<File>();
        if (files == null) {
            return oldFiles;
        }
        for (var f: files) {
            BasicFileAttributes fat = null;
            try {
                fat = Files.readAttributes(Path.of(f.getAbsolutePath()),
                        BasicFileAttributes.class);
                var creationTime = fat.creationTime().toMillis();
                var timestamp = new Timestamp(System.currentTimeMillis());
                var now = timestamp.getTime();
                if (creationTime <= now - secondsOld * 1000) {
                    oldFiles.add(f);
                }
            } catch (IOException e) {
                //  ignore errors when file creation time cannot be retrieved
                e.printStackTrace();
            }
        }
        return oldFiles;
    }
}

We wrap the exception handling in a try-catch and when files are failed to read for some reasons, we simply ignore that particular file. This utility is useful if you want to purge old cache files periodically.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
323 words
Last Post: Two PHP Functions to Check HTTP Response Code (Status) of a URL using Curl
Next Post: Teaching Kids Programming - Converting Spreadsheet Column Titles to Number

The Permanent URL is: Getting a List of Old Files in a Directory in Java by Comparing the Files Creation Time

Leave a Reply