public static File findLatesFile(final String path, final String keyword) { return findLatesDirectory(path, keyword, false); } public static File findLatesDirectory(final String path, final String keyword) { return findLatesDirectory(path, keyword, true); } public static File findLatesDirectory(final String path, final String keyword, final boolean isDirectory) { if (StringUtils.isBlank(path)) { throw new RuntimeException("Path is invalid."); } File srcFolder = new File(path); if (srcFolder.isFile()) { throw new RuntimeException("Path is not a folder."); } File[] files = null; if (StringUtils.isNotBlank(keyword)) { files = srcFolder.listFiles(new FileFilter() { // search files for // keyword public boolean accept(File pathname) { if (isDirectory) { return pathname.getName().contains(keyword); } return pathname.isFile() && pathname.getName().contains(keyword); } }); } else { files = srcFolder.listFiles(); } if (!CollectionUtils.isEmpty(Arrays.asList(files))) { Arrays.sort(files, Collections.reverseOrder()); return files[0]; } throw new RuntimeException("Directory is empty. " + path); }