Benji Weiss
Benji Weiss

Reputation: 416

How to get oldest file in a directory using Java

Are there any methods for obtaining the oldest file in a directory using java? I have a directory i write log files to and would like to delete log files after ive recorded over 500 log files (but only want to delete the oldest ones).

The only way i could conceive myself is:

The inconvenient aspect of this logic is that i would have to loop the log directory every time i want to get the oldest files and that does not seem the most efficient.

i expected the java.io.File library would have a method to get the oldest file in a directory but it doesnt seem to exist, or i havent found it. If there is a method for obtaining the oldest file in a directory or a more convenient approach in programming the solution, i would love to know.

Thanks

Upvotes: 2

Views: 4534

Answers (2)

Benji Weiss
Benji Weiss

Reputation: 416

Based off of @Yoda comment, i figured i would answer my own question.

public static void main(String[] args) throws IOException {
    File directory = new File("/logFiles");
    purgeLogFiles(directory);
}

public void purgeLogFiles(File logDir){
    File[] logFiles = logDir.listFiles();
    long oldestDate = Long.MAX_VALUE;
    File oldestFile = null;
    if( logFiles != null && logFiles.length >499){
        //delete oldest files after theres more than 500 log files
        for(File f: logFiles){
            if(f.lastModified() < oldestDate){
                oldestDate = f.lastModified();
                oldestFile = f;
            }               
        }

        if(oldestFile != null){
            oldestFile.delete();
        }
    }
}

Upvotes: 4

Not a JD
Not a JD

Reputation: 1902

Unfortunately, you're gonna have to just walk the filesystem. Something like:

public static void main(String[] args) throws IOException {
    String parentFolder = "/var/log";
    int numberOfOldestFilesToFind = 5;

    List<Path> oldestFiles = findOldestFiles(parentFolder, numberOfOldestFilesToFind);

    System.out.println(oldestFiles);
}

private static List<Path> findOldestFiles(String parentFolder, int numberOfOldestFilesToFind) throws IOException {
    Comparator<? super Path> lastModifiedComparator = 
            (p1, p2) -> Long.compare(p1.toFile().lastModified(),
                                     p2.toFile().lastModified());

    List<Path> oldestFiles = Collections.emptyList();

    try (Stream<Path> paths = Files.walk(Paths.get(parentFolder))) {
        oldestFiles = paths.filter(Files::isRegularFile)
                           .sorted(lastModifiedComparator)
                           .limit(numberOfOldestFilesToFind)
                           .collect(Collectors.toList());
    }

    return oldestFiles;
}

Upvotes: 1

Related Questions