FileTreeWalker { public static List<File> listFile(File rootDir) { return Files.fileTreeTraverser().preOrderTraversal(rootDir).filter(Files.isFile()).toList(); }  static List<File> listAll(File rootDir); static List<File> listFile(File rootDir); static List<File> listFileWithExtension(final File rootDir, final String extension); static List<File> listFileWithWildcardFileName(final File rootDir, final String fileNamePattern); static List<File> listFileWithRegexFileName(final File rootDir, final String regexFileNamePattern); static List<File> listFileWithAntPath(final File rootDir, final String antPathPattern); static TreeTraverser<File> fileTreeTraverser();  }
@Test public void listFile() throws IOException { File tmpDir = FileUtil.createTempDir().toFile(); List<File> all = FileTreeWalker.listAll(tmpDir); assertThat(all).hasSize(1); List<File> files = FileTreeWalker.listFile(tmpDir); assertThat(files).hasSize(0); FileUtil.touch(FilePathUtil.concat(tmpDir.getAbsolutePath(), "tmp-" + RandomUtil.nextInt()) + ".tmp"); FileUtil.touch(FilePathUtil.concat(tmpDir.getAbsolutePath(), "tmp-" + RandomUtil.nextInt()) + ".abc"); String childDir = FilePathUtil.concat(tmpDir.getAbsolutePath(), "tmp-" + RandomUtil.nextInt()); FileUtil.makesureDirExists(childDir); FileUtil.touch(FilePathUtil.concat(childDir, "tmp-" + RandomUtil.nextInt()) + ".tmp"); all = FileTreeWalker.listAll(tmpDir); assertThat(all).hasSize(5); files = FileTreeWalker.listFile(tmpDir); assertThat(files).hasSize(3); files = FileTreeWalker.listFileWithExtension(tmpDir, "tmp"); assertThat(files).hasSize(2); files = FileTreeWalker.listFileWithExtension(tmpDir, "tp"); assertThat(files).hasSize(0); files = FileTreeWalker.listFileWithWildcardFileName(tmpDir, "*.tmp"); assertThat(files).hasSize(2); files = FileTreeWalker.listFileWithWildcardFileName(tmpDir, "*.tp"); assertThat(files).hasSize(0); files = FileTreeWalker.listFileWithRegexFileName(tmpDir, ".*\\.tmp"); assertThat(files).hasSize(2); files = FileTreeWalker.listFileWithRegexFileName(tmpDir, ".*\\.tp"); assertThat(files).hasSize(0); files = FileTreeWalker.listFileWithAntPath(tmpDir, "**" + File.separator + "*.tmp"); assertThat(files).hasSize(2); files = FileTreeWalker.listFileWithAntPath(tmpDir, "*" + File.separator + "*.tmp"); assertThat(files).hasSize(1); files = FileTreeWalker.listFileWithAntPath(tmpDir, "*.tp"); assertThat(files).hasSize(0); FileUtil.deleteDir(tmpDir); assertThat(FileUtil.isDirExists(tmpDir)).isFalse(); }