• java文件遍历


    /**
         * 深度搜索遍历文件夹
         * 
         * @param dirPath
         * @param list
         */
        public static void dfsListFile(String dirPath, List<String> list) {
            File file = new File(dirPath);
            File[] files = file.listFiles();
            for (File tmpFile : files) {
                if (tmpFile.isDirectory()) {
                    dfsListFile(tmpFile.getAbsolutePath(), list);
                } else {
                    list.add(tmpFile.getAbsolutePath());
                }
            }
        }
    
        /**
         * 广度搜索遍历文件夹
         * 
         * @param dirPath
         * @param list
         */
        public static void bfsListFile(String dirPath, List<String> list) {
            File file = new File(dirPath);
            File[] fs = file.listFiles();
            Queue<File> queue = new LinkedList<>();
    
            // 遍历第一层
            for (File f : fs) {
                // 把第一层文件夹加入队列
                if (f.isDirectory())
                    queue.offer(f);
                else
                    list.add(f.getAbsolutePath());
            }
            // 逐层搜索下去
            while (!queue.isEmpty()) {
                // 从队列头取一个元素
                File fileTemp = queue.poll();
                File[] fileListTemp = fileTemp.listFiles();
                for (File f : fileListTemp) {
                    if (f.isDirectory())
                        queue.offer(f);
                    else
                        list.add(f.getAbsolutePath());
                }
            }
    
        }
  • 相关阅读:
    字符串系列复习
    点分治总结
    LCT总结
    网络流总结
    centOS7下安装GUI图形界面
    周记 2014.10.8
    周记 2014.9.28
    周记 2014.9.20
    tar命令
    [转]bit与byte
  • 原文地址:https://www.cnblogs.com/zincredible/p/11847016.html
Copyright © 2020-2023  润新知