如何统计java代码行数_Java 统计一个项目中src下的所有 .java 文件的代码行数, 注释行数, 空行数...

packagecom.maya.hanqi.coder;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.io.IOException;public classCodeCounter {private static Integer code = 0;private static Integer codeComments = 0;private static Integer codeBlank = 0;public static voidmain(String[] args) {

File file= new File("E:\\EclipseSpace\\JavaWorkPlace\\JavaWeb_TestJDBC\\src\\com\\hanqi");

factFiles(file);

System.out.println("代码行数" +code);

System.out.println("空白行数" +codeBlank);

System.out.println("注释行数" +codeComments);

}public static voidfactFiles(File file) {

BufferedReader br= null;

String s= null;if(file.isDirectory()) {

File[] files=file.listFiles();for(File f : files) {

factFiles(f);

}

}else{try{

br= new BufferedReader(newFileReader(file));boolean comm = false;while((s = br.readLine()) != null) {if(s.startsWith("/*") && s.endsWith("*/")) {

codeComments++;

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

codeComments++;

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

codeComments++;

comm= true;

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

codeComments++;

comm= false;

}else if(comm) {

codeComments++;

}else if(s.trim().length() < 1) {

codeBlank++;

}else{

code++;

}

}

br.close();

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值