Sometimes, we need to get a list of files exist within a directory as per our criteria like filename, file extension, last modified date or other attributes. It reduces the overhead to filter them after getting the list using our code. To achieve this, Java provides an interface - FileFilter which has a single method prototype as boolean accept(File file). To apply our filtering criteria, we have to override this method and if our logic returns true for a file then only the file will be included in the list otherwise not.
Example :-
Example :-
import java.io.*; import java.util.*; public class FilteredFileList { public static File[] getFilteredFileList(File folder, final String filenameFilter, Date lastModifiedDateFilter) { File[] listOfFiles = folder.listFiles(new FileFilter(){ public boolean accept(File file) { //file is the object against which we are applying our logic. //You can put your logic using filenameFilter and check whether //the file's name comes under your criteria or not. //To check against the lastmodified date you can use lastModifiedDateFilter. return true; }}); return listOfFiles; } }