自己编的代码统计程序

统计一个文件夹中所有.java文件里有多少空白行,多少代码行,多少注释行

package com.jason;


import java.io.*;

import java.util.regex.*;


public class CoutLines {

static int commentLines = 0;

static int blackLines = 0;

static int codeLines = 0;

public static void main(String args[]) {

File f= new File("D:\\code");

File [] fileList = f.listFiles();

for(int i =0;i<fileList.length;i++) {

if(fileList[i].getName().endsWith("java")){

parse(fileList[i]);

}

}

System.out.println("代码行有:"+codeLines);

System.out.println("空白行有:"+blackLines);

System.out.println("注释行有:"+commentLines);


}


private static void parse(File f) {

boolean flag = false;

Pattern p = Pattern.compile("^[\\s&&[^\\n]]*");

Matcher m = null;

try {

BufferedReader br = new BufferedReader(new FileReader(f));

String s = "";

try {

while((s = br.readLine())!=null){

s = s.trim();

m = p.matcher(s);

if(s.startsWith("/*")&&!s.endsWith("*/")){

commentLines++;

flag = true;

}

else if(true == flag) {

commentLines++;

if(s.endsWith("*/")){

flag = false;

}

}

else if(s.startsWith("/*")&&s.endsWith("*/")){

commentLines ++;

}

else if(s.startsWith("//")){

commentLines++;

}

else if(m.matches()){

blackLines++;

}

else

codeLines++;

}

} catch (IOException e) {

e.printStackTrace();

}

catch (FileNotFoundException e) {

e.printStackTrace();

}

}

}

 

posted @ 2010-01-28 10:50  zpdlut  阅读(440)  评论(2编辑  收藏  举报