java 代码统计_Java 代码行统计(转)

packagecodecounter;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.io.IOException;importjava.util.regex.Matcher;importjava.util.regex.Pattern;public classCodeCounter {static long commentLine = 0;static long whiteLine = 0;static long normalLine = 0;static long totalLine = 0;static boolean comment = false;static final String base_dir = "D:\\javap\\qdgs\\qdgs_gzfw\\src\\java\\qdgsgzfw";public static voidmain(String[] args) {

File file= new File(base_dir); //在这里输入需要统计的文件夹路径

getChild(file);

System.out.println("有效代码行数: " +normalLine);

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

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

System.out.println("总代码行数: " +totalLine);

}private static void getChild(File child) { //遍历子目录

if (child.getName().matches(".*\\.java$")) { //只查询java文件

try{

BufferedReader br= new BufferedReader(newFileReader(child));

String line= "";while ((line = br.readLine()) != null) {

parse(line);

}

}catch(FileNotFoundException e1) {

e1.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}

}if (child.listFiles() != null) {for(File f : child.listFiles()) {

getChild(f);

}

}

}private static voidparse(String line) {

line=line.trim();

totalLine++;if (line.length() == 0) {

whiteLine++;

}else if(comment) {

commentLine++;if (line.endsWith("*/")) {

comment= false;

}else if (line.matches(".*\\*/.+")) {

normalLine++;

comment= false;

}

}else if (line.startsWith("//")) {

commentLine++;

}else if (line.matches(".+//.*")) {

commentLine++;

normalLine++;

}else if (line.startsWith("/*") && line.matches(".+\\*/.+")) {

commentLine++;

normalLine++;if(findPair(line)) {

comment= false;

}else{

comment= true;

}

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

commentLine++;

comment= true;

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

commentLine++;

comment= false;

}else if (line.matches(".+/\\*.*") && !line.endsWith("*/")) {

commentLine++;

normalLine++;if(findPair(line)) {

comment= false;

}else{

comment= true;

}

}else if (line.matches(".+/\\*.*") && line.endsWith("*/")) {

commentLine++;

normalLine++;

comment= false;

}else{

normalLine++;

}

}private static boolean findPair(String line) { //查找一行中/*与*/是否成对出现

int count1 = 0;int count2 = 0;

Pattern p= Pattern.compile("/\\*");

Matcher m=p.matcher(line);while(m.find()) {

count1++;

}

p= Pattern.compile("\\*/");

m=p.matcher(line);while(m.find()) {

count2++;

}return (count1 ==count2);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
代码统计工具使用说明 代码统计工具用Java语言实现,可以对C\C++、Java的源代码统计统计结果包括:总数、空数、注释数和代码数,统计结果可以以表格的形式显示给用户或另存为文件。 一 执环境: 操作系统:WindowsNT/2000; 执环境:JDK1.3。 二 安装: 1)将code.zip解压缩; 3)设置环境变量: 在 开始—〉设置—〉控制面板—〉系统—〉高级—〉环境变量 中设置环境变量: PATH=[code.zip的安装目录]\jre\bin CLASSPATH=[code.zip的安装目录]\class 三 运: 直接运(双击)在源程序[code.zip的安装目录]\class目录下的CodeStatistic.bat 批处理文件。 〈注〉:如果程序不能正常运,有可能是前面系统环境变量PATH、CLASSPATH设置的问题,请检查添加的路径是否正确,确定正确还不能正常运,请注销当前用户后再启动程序。 四 使用方法: 1. 代码统计工具开始工作后,点击窗口下方的“添加”按钮会以对话框的形式浏览硬盘目录,现在可以添加要进统计的C\C++、Java源程序, 2. 选定文件后,会在代码统计工具主界面的列表框中显示已选定的文件名和路径; 3. 点击“添加”按钮继续添加需统计的文件或点击“删除”按钮删除已选文件; 4. 需统计的文件选定后,点击“统计”按钮开始统计,弹出“代码统计结果”窗口以表格形式显示统计结果; 5. 在“代码统计结果” 窗口中可选择将统计结果另存为文件形式; 6. 点击“关闭”按钮关闭“代码统计结果” 窗口,回到主窗口; 7. 继续其它统计活动或点击“退出”按钮退出代码统计工具。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值