您的位置:首页 > 编程语言

利用正则表达式编写的代码统计工具

2015-12-30 01:20 344 查看
package regexp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FilePermission;
import java.io.FileReader;
import java.text.Normalizer;

public class countline {

static long normalLines=0;
static long commentLines=0;
static long whiteLines=0;

public static void main(String[] args) {
// TODO Auto-generated method stub
File f=new File("F:\\src\\java\\dao\\impl");//要统计的代码存放的所在的文件夹路径
File[] codeFiles=f.listFiles();
for(File child: codeFiles){
if(child.getName().matches(".*\\.java$")){
parse(child);
}
}
System.out.println("normalLines:"+normalLines);
System.out.println("commentLines:"+commentLines);
System.out.println("whiteLines:"+whiteLines);
}

private static void parse(File f) {
BufferedReader bReader=null;
boolean comment=false;
try {
bReader=new BufferedReader(new FileReader(f));
String line=" ";
while((line=bReader.readLine())!=null)
{
line=line.trim();
if(line.matches("^[\\s&&[^\\n]]*$")){
whiteLines++;

}else if(line.startsWith("/*")&&!line.endsWith("*/")){
commentLines++;
comment=true;

}else if(line.startsWith("/*")&&line.endsWith("*/")){
commentLines++;
}
else if(true==comment){
commentLines++;
if(line.endsWith("*/")){
comment=false;
}
}else if(line.endsWith("//")){
commentLines++;
}

else {
normalLines++;
}
}

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
if(bReader!=null){
try {
bReader.close();bReader=null;
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息