zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    测试方法与步骤
    团队项目需求分析
    第一次个人作业
    3种shell自动交互的方法
    mysql用户管理
    build web application with golang
    安卓中的LINUX内核
    结对项目小结
    关于aria2-yaaw下载软件
    软工结对项目预览
  • 原文地址:https://www.cnblogs.com/freshier/p/4756156.html
Copyright © 2011-2022 走看看