package com.example.demo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class Test {
    static int count =0;
    public static void main(String[] args) {
        String pre = "";
        String path = "G:\\test";
        printDir(pre,path);
        System.out.println(count);
    }

    static void printDir(String pre,String path) {
        File dir = new File(path);
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                pre =pre +"--";
                printDir(pre,file.getPath());
            }else{
                printContent(file);
            }
        }
    }

    private static void printContent(File file) {
        StringBuilder result = new StringBuilder();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));
            String s;
            while((s = br.readLine())!=null){
                count++;
                result.append(System.lineSeparator()+s);
            }
            br.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        System.out.println(result);
    }
}

 

posted on 2023-03-23 14:46  song.yan  阅读(35)  评论(0编辑  收藏  举报