public class Tongjidaima { private static int i;//代碼總行數 private static int j;//文件個數 public static void main(String[] args) throws IOException { File file = new File("F:\\eclipsework\\Zhansen");//需要統計行數的文件夾路徑 traverseFiles(file);//調用遞歸方法查看.java文件,用於統計行數 System.out.println("所寫文件個數:"+j); System.out.println("所寫代碼總行數:"+i); } public static void traverseFiles(File file) throws IOException{ if(!file.exists()){//文件不存在 return; } if(!file.isDirectory()){//判斷是否為文件 String filename = file.getName(); if(filename.endsWith(".java")){//判斷是否是.java文件 j++; BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); String string =null; while ((string = bufferedReader.readLine()) != null) { i++;//讀取行數 } }else return; } File[] files =file.listFiles();//讀取文件夾的子文件或子文件夾 if (files == null || files.length == 0) { return; } for(File file2 : files){//如果是文件夾遞歸調用方法遍歷文件 traverseFiles(file2); } } }