08-天亮大数据经典JavaSe笔试题系列之给定任意目录递归遍历问题

  • 题目:
    • 利用递归方法,实现将任意给定的文件夹下所有文件及目录的绝对路径字符串形式加入到集合,并输出之。
package com.tl.job005.test.io;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileOperatorUtil {
    public static List<String> getAllFileList4Other(String localDirPath,
            List<String> resultList) {
        File fileObj = new File(localDirPath);
        if (!fileObj.exists()) {
            return null;
        }
        if (fileObj.isDirectory()) {
            File[] subFileArray = fileObj.listFiles();
            for (File oneFile : subFileArray) {
                getAllFileList4Other(oneFile.toString(), resultList);
            }
            resultList.add(fileObj.toString());
        } else {
            resultList.add(fileObj.toString());
        }
        return resultList;
    }

    public static List<String> getAllFileList4Standard(String localDirPath) {
        File fileObj = new File(localDirPath);
        if (!fileObj.exists()) {
            return null;
        }
        List<String> resultList = new ArrayList<String>();
        if (fileObj.isDirectory()) {
            File[] subFileArray = fileObj.listFiles();
            for (File oneFile : subFileArray) {
                resultList.addAll(getAllFileList4Standard(oneFile.toString()));
            }
            resultList.add(fileObj.toString());
        } else {
            resultList.add(fileObj.toString());
        }
        return resultList;
    }

    public static void main(String[] args) {
        String localDirPath = "E:\\最后冲刺30天";
        List<String> allFileList = new ArrayList<String>();
//        getAllFileList(localDirPath, allFileList);
        allFileList=getAllFileList4Standard(localDirPath);
        for (String pathString : allFileList) {
            System.out.println(pathString);
        }
    }
}


天亮教育是一家从事大数据云计算、人工智能、教育培训、产品开发、咨询服务、人才优选为一体的综合型互联网科技公司。
公司由一批BAT等一线互联网IT精英人士创建,
以"快乐工作,认真生活,打造IT职业技能教育的一面旗帜"为愿景,胸怀"让天下没有难找的工作"使命,
坚持"客户第一、诚信、激情、拥抱变化"的价值观,
全心全意为学员赋能提效,践行技术改变命运的初心。

更多学习讨论, 请加入
官方-天亮大数据交流-366784928
群二维码:
这里写图片描述
官方-天亮web前端交流-972788995
群二维码:
在这里插入图片描述

欢迎关注天亮教育公众号,大数据技术资料与课程、招生就业动态、教育资讯动态、创业历程分享一站式分享,官方微信公众号二维码:
在这里插入图片描述

天亮教育大数据官方群318971238,
天亮教育web前端官方群318971238,
爬虫、nlp技术qq群320349384
hadoop & spark & hive技术群297585251
教育培训官网:http://myhope365.com
项目研发业务尚云科技官网:http://shangyuninfo.com/
天亮教育公开课-从小白到大佬修成记-全系列视频地址:http://myhope365.com/news/index?id=66

猜你喜欢

转载自blog.csdn.net/erliang20088/article/details/89735819