import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class CountCode { static int nomalCount = 0;//代码行 static int commonCount = 0;//注释行 static int whiteCount = 0;//空白行 public static void main(String[] args) { File f = new File("D:\\testjava"); File[] files = f.listFiles(); for(int i=0;i<files.length;i++){ File ff = files[i]; System.out.println(ff.getName()); if(ff.getName().matches(".*\\.java$")){ System.out.println("000000"); parse(ff); } } System.out.println("nomoalCount="+nomalCount); System.out.println("commonCount="+commonCount); System.out.println("whiteCount="+whiteCount); } private static void parse(File f) { BufferedReader br = null; boolean flag = false; try { br = new BufferedReader(new FileReader(f)); String line = ""; while((line = br.readLine()) != null){
line = line.trim(); if(line.matches("^[\\s&&[^\\n]]*$")){//空白行正则表达式 whiteCount++; }else if(line.startsWith("/*")&& !line.endsWith("*/")){ commonCount++; flag = true; }else if(true == flag){ commonCount++; if(line.endsWith("*/")){ System.out.println("sdfsd"); flag = false; } }else{ nomalCount++; } } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }