Java 读取目录下的所有文件

 1 package util;
 2 
 3 import java.io.File;
 4 import java.util.ArrayList;
 5 import java.util.List;
 6 
 7 import org.apache.commons.lang3.StringUtils;
 8 
 9 public class FileUtils {
10     public static void main(String[] args) {
11         List<File> files = FileUtils
12                 .getAllFiles("D:" + File.separator + "Java");
13         for (File file : files) {
14             System.out.println(file);
15         }
16     }
17 
18     /**
19      * 列出目录下的所有文件.
20      * 
21      * @param path
22      * @return
23      */
24     private static List<File> getAllFiles(String path) {
25         List<File> files = new ArrayList<File>();
26         if (!StringUtils.isNotEmpty(path)) {
27             return files;
28         }
29         File root = new File(path);
30         if (root.exists()) {
31             if (root.isDirectory()) {
32                 File[] childFiles = root.listFiles();
33                 for (File childFile : childFiles) {
34                     files.addAll(getAllFiles(childFile.getAbsolutePath()));
35                 }
36             } else {
37                 files.add(root);
38             }
39         }
40         return files;
41     }
42 }
原文地址:https://www.cnblogs.com/freshier/p/4756156.html