zoukankan      html  css  js  c++  java
  • Java基础

    遍历目录下所有文件

    1.非递归方式

        public static void folderMethod1(String path) {
            int fileNum = 0, folderNum = 0;
            File file = new File(path);
            LinkedList<File> list = new LinkedList<>();
    
            if (file.exists()) {
                if (null == file.listFiles()) {
                    return;
                }
                list.addAll(Arrays.asList(file.listFiles()));
                while (!list.isEmpty()) {
                    File[] files = list.removeFirst().listFiles();
                    if (null == files) {
                        continue;
                    }
                    for (File f : files) {
                        if (f.isDirectory()) {
                            System.out.println("文件夹:" + f.getAbsolutePath());
                            list.add(f);
                            folderNum++;
                        } else {
                            System.out.println("文件:" + f.getAbsolutePath());
                            fileNum++;
                        }
                    }
                }
            } else {
                System.out.println("文件不存在!");
            }
            System.out.println("文件夹数量:" + folderNum + ",文件数量:" + fileNum);
        }

    2.递归方式

        public static void folderMethod2(String path) {
            File file = new File(path);
            if (file.exists()) {
                File[] files = file.listFiles();
                if (null != files) {
                    for (File file2 : files) {
                        if (file2.isDirectory()) {
                            System.out.println("文件夹:" + file2.getAbsolutePath());
                            folderMethod2(file2.getAbsolutePath());
                        } else {
                            System.out.println("文件:" + file2.getAbsolutePath());
                        }
                    }
                }
            } else {
                System.out.println("文件不存在!");
            }
        }
  • 相关阅读:
    layui 相关知识
    ideal debug 启动不起来
    删除命令 rm -rf maven-project-jxcg.zip 解压 unzip maven-project-jxcg.zip
    vsb 配置
    cmd dos
    switch
    Element UI 框架搭建
    mysql 远程连接设置
    YApi可视化接口管理平台 接口
    报403错误
  • 原文地址:https://www.cnblogs.com/helios-fz/p/11023205.html
Copyright © 2011-2022 走看看