Here you can find the source of getMostRecentFile(File dir, java.io.FileFilter filter)
Parameter | Description |
---|---|
dir | The directory to search in. |
filter | The FileFilter to be used to limit what files we look at (may be null). |
public static File getMostRecentFile(File dir, java.io.FileFilter filter)
//package com.java2s; /*// w w w . j a v a 2s. c o m * Copyright 1997-2016 Unidata Program Center/University Corporation for Atmospheric Research * Copyright 2010-2015 Jeff McWhirter * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or (at * your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser * General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ import java.io.File; public class Main { /** * Find the youngest file in the given directory. * * @param dir The directory to search in. * @return The most recent file (or null if none found). */ public static File getMostRecentFile(File dir) { return getMostRecentFile(dir, (java.io.FileFilter) null); } /** * Find the youngest file in the given directory that matches the given {@link FileFilter}. * * @param dir The directory to search in. * @param filter The {@link FileFilter} to be used to limit what files we look at (may be null). * @return The most recent file (or null if none found). */ public static File getMostRecentFile(File dir, java.io.FileFilter filter) { if (!dir.isDirectory()) { throw new IllegalArgumentException("Not a directory:" + dir); } File[] list = ((filter == null) ? dir.listFiles() : dir.listFiles(filter)); File latestFile = null; long latest = Long.MIN_VALUE; for (int i = 0; i < list.length; i++) { long tmp = list[i].lastModified(); if (tmp > latest) { latestFile = list[i]; latest = tmp; } } return latestFile; } /** * Find the youngest file in the given directory that matches the given {@link FileFilter}. * * @param dir The directory to search in. * @param filter The filter to be used to limit what files we look at (may be null). * @return The most recent file (or null if none found). */ public static File getMostRecentFile(File dir, final javax.swing.filechooser.FileFilter filter) { return getMostRecentFile(dir, wrapFilter(filter)); } /** * Create a javaio FileFilter from the filechooser package file filter. * * @param filter The filechooser file filter * * @return The javaio FileFilter. */ public static java.io.FileFilter wrapFilter(final javax.swing.filechooser.FileFilter filter) { return new java.io.FileFilter() { public boolean accept(File f) { return ((filter == null) ? true : filter.accept(f)); } }; } }