Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Get the latest file in a directory

Thread view: 
steve.chambers@gmail.com - 19 Jul 2006 10:32 GMT
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
Ingo R. Homann - 19 Jul 2006 10:56 GMT
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
TechBookReport - 19 Jul 2006 11:33 GMT
> Hi,
>
[quoted text clipped - 3 lines]
> Ciao,
> Ingo

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

Signature

TechBookReport Java - http://www.techbookreport.com/JavaIndex.html

steve.chambers@gmail.com - 19 Jul 2006 14:12 GMT
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! :-)

> > Hi,
> >
[quoted text clipped - 9 lines]
> --
> TechBookReport Java - http://www.techbookreport.com/JavaIndex.html
Roland de Ruiter - 19 Jul 2006 18:40 GMT
> 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();
        }
    }
}

Signature

Regards,

Roland



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.