001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.sys;
017    
018    import java.io.File;
019    import java.io.FilenameFilter;
020    
021    /**
022     * This class provides a set of facilities that can be used to work with files
023     */
024    public class FileUtil {
025        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FileUtil.class);
026    
027        /**
028         * In directory looks for a pattern matching filenameFilter and returns the filename with the highest lastModified()
029         * @param directory
030         * @param filenameFilter to filter filenames in batchFileDirectoryName for
031         * @return File with highest lastModified()
032         */
033        public static File getNewestFile(File directory, FilenameFilter filenameFilter) {
034            File newestFile = null;
035            
036            File[] directoryListing = directory.listFiles(filenameFilter);
037            if (directoryListing == null || directoryListing.length == 0) {
038                return null;
039            } else {
040                for (int i = 0; i < directoryListing.length; i++) {
041                    File file = directoryListing[i];
042                    if (newestFile == null) {
043                        newestFile = file;
044                    } else {
045                        if (newestFile.lastModified() < file.lastModified()){
046                            newestFile = file;                        
047                        }
048                    }
049                }
050            }
051            
052            return newestFile;
053        }
054    }