Get the latest file in a directory

Discussion in 'Java' started by steve.chambers@gmail.com, Jul 19, 2006.

  1. Guest

    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
    , Jul 19, 2006
    #1
    1. Advertising

  2. 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
    Ingo R. Homann, Jul 19, 2006
    #2
    1. Advertising

  3. Ingo R. Homann wrote:
    > 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().

    --
    TechBookReport Java - http://www.techbookreport.com/JavaIndex.html
    TechBookReport, Jul 19, 2006
    #3
  4. Guest

    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! :)

    TechBookReport wrote:

    > Ingo R. Homann wrote:
    > > 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().
    >
    > --
    > TechBookReport Java - http://www.techbookreport.com/JavaIndex.html
    , Jul 19, 2006
    #4
  5. On 19-7-2006 15:12, wrote:
    > 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();
    }
    }
    }


    --
    Regards,

    Roland
    Roland de Ruiter, Jul 19, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Damien
    Replies:
    0
    Views:
    1,554
    Damien
    Aug 4, 2005
  2. Hank
    Replies:
    3
    Views:
    380
    Tim Golden
    Oct 30, 2003
  3. Replies:
    1
    Views:
    113
    Bob Barrows [MVP]
    Jun 24, 2005
  4. Rajeswar reddy Gaulla
    Replies:
    2
    Views:
    140
  5. Mmcolli00 Mom
    Replies:
    2
    Views:
    122
    William James
    Feb 10, 2009
Loading...

Share This Page