Discuss / Java / 遍历目录文件

遍历目录文件

Topic source

/**

     * 递归实现

     *

     * @param file

     * @param depth

     */

    public static void pathWalk(File file, int depth) {

        if (file.exists()) {

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < depth; i++) {

                sb.append(" ");

            }

            System.out.println(sb.toString() + file.getName() + (file.isDirectory() ? "/" : ""));

            if (file.isDirectory()) {

                File[] files = file.listFiles();

                for (File file1 : files) {

                        pathWalk(file1, depth + 1);

                }

            }

        }

    }


  • 1

Reply