Get the latest file in a directory

S

steve.chambers

Does anyone know if there is a way in Java to get the latest (i.e. most
recently modified) file in a directory? I have looked at File.list()
but there doesn't seem to be a way of ordering the results. A quick
surf of the web hasn't given me much either...but surely there must be
a way.

Any help/ideas would be much appreciated!

Cheers,
Steve
 
I

Ingo R. Homann

Hi,

of course, there is a possibilty: List all files and then search the
latest one (using File#lastModified()). Its just that simple. :)

Ciao,
Ingo
 
T

TechBookReport

Ingo said:
Hi,

of course, there is a possibilty: List all files and then search the
latest one (using File#lastModified()). Its just that simple. :)

Ciao,
Ingo

Of course you can also set up a Comparator to do the sorting based on
lastModified().
 
S

steve.chambers

Great, didn't spot that method at first. Have used it & it seems to
work. Comparator's a good idea but I needed to weed out the folders
from the files so just used a loop in the end. Thank you both for the
help! :)
 
R

Roland de Ruiter

Great, didn't spot that method at first. Have used it & it seems to
work. Comparator's a good idea but I needed to weed out the folders
from the files so just used a loop in the end. Thank you both for the
help! :)
You can use a FileFilter (or FilenameFilter) to select the appropriate
files in the File#listFiles method.
Here's an example that uses both a FileFilter to filter out directories
and a Comparator to order the files in descending modification date order:

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

public class LastFile {

public static void main(String[] args) {
// FileFilter that accepts all files except directories
FileFilter noDirectories = new FileFilter() {
public boolean accept(File f) {
return !f.isDirectory();
}
};

// Comparator for modification date in descending order
Comparator<File> descendingOnModificationDate =
new Comparator<File>() {
public int compare(File f1, File f2) {
long diff = f1.lastModified() - f2.lastModified();
int returnValue;
if (diff < 0L) {
returnValue = -1;
} else if (diff > 0L) {
returnValue = +1;
} else {
assert diff == 0L;
returnValue = 0;
}
return -returnValue; // +returnValue for ascending order
}
};

// Directory to list (here: user's home directory)
File directory = new File(System.getProperty("user.home", "."));

// Obtain non-directory files in the directory
File[] filesInDirectory = directory.listFiles(noDirectories);

// Sort the list on modification date in descending order
Arrays.sort(filesInDirectory, descendingOnModificationDate);

// Print the result
for (File file : filesInDirectory) {
System.out.print(new Date(file.lastModified()));
System.out.print('\t');
System.out.print(file);
System.out.println();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top