java 文件 读取目录下的所有文件(包括子目录)

package com.jxtech.filetool;

import java.io.File;

public class FileTool {
    public static void main(String[] args) throws Exception {
        // 递归显示C盘下所有文件夹及其中文件
        File root = new File("o:/GZX");
        showAllFiles(root);
    }

    final static void showAllFiles(File dir) throws Exception {
        File[] fs = dir.listFiles();
        for (int i = 0; i < fs.length; i++) {
            System.out.println(fs[i].getAbsolutePath());
            if (fs[i].isDirectory()) {
                try {
                    showAllFiles(fs[i]);
                } catch (Exception e) {
                }
            }
        }
    }
}

猜你喜欢

转载自awen7916.iteye.com/blog/2242859