Here you can find the source of getFilesOfDirectory(File directory)
public static List<File> getFilesOfDirectory(File directory)
//package com.java2s; /* Copyright (C) 2013-2015 BMW Group * Author: Manfred Bathelt (manfred.bathelt@bmw.de) * Author: Juergen Gehring (juergen.gehring@bmw.de) * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ import java.io.File; import java.util.Collections; import java.util.LinkedList; import java.util.List; public class Main { public static List<File> getFilesOfDirectory(File directory) { return getFilesOfDirectory(directory, null); }/*ww w.j av a 2 s. c om*/ public static List<File> getFilesOfDirectory(File directory, String fileExtension) { List<File> fileList = new LinkedList<>(); if (!directory.isDirectory()) { return fileList; } for (File fileInDirectory : directory.listFiles()) { if (fileInDirectory.isDirectory()) { fileList.addAll(getFilesOfDirectory(fileInDirectory, fileExtension)); } else if (fileInDirectory.isFile() && (null == fileExtension || fileInDirectory.getName().endsWith("." + fileExtension))) { fileList.add(fileInDirectory); } } Collections.sort(fileList, new java.util.Comparator<File>() { @Override public int compare(File file1, File file2) { return file1.getAbsolutePath().compareTo(file2.getAbsolutePath()); } }); return fileList; } }