class="java"> package test; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; public class Test { private static int sum = 0; public static void main(String[] args) throws Exception{ File f = new File("E:\\src"); listFile(f); System.out.println("代码一共:"+sum+"行"); } public static void listFile(File file) throws Exception{ if(file.isDirectory()){ File list[] = file.listFiles(); for(File ff:list){ listFile(ff); if(ff.getPath().endsWith(".java")){ sum+=number(ff); } } } } public static int number(File file) throws Exception{ InputStream is = new FileInputStream(file); LineNumberReader lnr = new LineNumberReader(new InputStreamReader(is,"UTF-8")); String line = null; int count = 0; while((line=lnr.readLine())!=null){ if(!line.equals("")){ count++; } } return count; } }