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); } }