JAVA项目代码统计工具源码

package com.moma.regex;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class codeCounter {
 private static long normalCode = 0;
 private static long commentCode = 0;
 private static long whitespaceCode = 0;

 
 public static void main(String[] args) {
  System.out.println("-------------------------------");
  System.out.print("      JAVA项目代码统计工具/n");
  System.out.println("        退出标记———>over");   
  System.out.println("    墨马工作室  QQ:343807757");
  System.out.println("-------------------------------/n/n");
  System.out.print("路径:>");
  BufferedReader bfr = new BufferedReader(
    new InputStreamReader(System.in));

  try {
   String path = bfr.readLine();
   while (path != null && !path.equals("over")) {
    normalCode = 0;
    commentCode = 0;
    whitespaceCode = 0;
    File file = new File(path);
    if (file.exists()) {

     File[] files = file.listFiles();
     for (File fileChild : files) {
      if (fileChild.getName().matches(".+//.java$")) {
       parse(fileChild);
      }
     }
     System.out.println("/n统计完毕 :/n");
     System.out.println("正常代码 有: " + normalCode);
     System.out.println("注   释  有: " + commentCode);
     System.out.println("空 白 行 有: " + whitespaceCode);
     System.out.println("-------------------------------/n");
     
     System.out.print("路径:>");
     path = bfr.readLine();
    } else {
     System.out.println("*目录不存在请重新输入*");
     System.out.print("路径:>");
     path = bfr.readLine();
    }
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 private static void parse(File file) {
  try {
   BufferedReader bfr = new BufferedReader(new FileReader(file));
   String line = "";
   while (null != (line = bfr.readLine())) {
    line = line.trim();
    // 判断是否为空行
    if (line.matches("^//s*$")) {
     whitespaceCode++;
    }
    // 判断是否为//注释
    else if (line.matches("^//s*//.*")) {
     commentCode++;
    }
    // 判断是否为/*注释
    else if (line.matches("^//s*///*.*")) {
     if (line.matches("^.*//*/.*"))
      commentCode++;
     else {
      commentCode++;
      while (null != (line = bfr.readLine())) {
       line = line.trim();
       commentCode++;
       if (line.matches("^.*//*/.*"))
        break;
      }
     }
    }
    // 其它为一般代码
    else
     normalCode++;
   }

  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

图片

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值