class FileUtil {

    public static File[] findFiles(File dir, FileFilter filter) throws FileNotFoundException {
        if (!dir.exists()) {
            throw new FileNotFoundException("Directory " + dir + " does not exist.");
        }

        if (!dir.isDirectory()) {
            throw new FileNotFoundException("File " + dir + " is not a directory.");
        }

        // being matching process, create array for returning results
        ArrayList<File> files = new ArrayList<File>();

        // get all files in this directory
        File[] allFiles = dir.listFiles();

        // were any files returned?
        if (allFiles != null && allFiles.length > 0) {
            // loop thru every file in the dir
            for (File f : allFiles) {
                // only match files, not a directory
                if (f.isFile()) {
                    // delegate matching to provided file matcher
                    if (filter.accept(f)) {
                        files.add(f);
                    }
                }
            }
        }

        // based on filesystem, order of files not guaranteed -- sort now
        File[] r = files.toArray(new File[0]);
        Arrays.sort(r);
        return r;
    }

    private  FileUtil();

}