美文网首页
统计项目代码行数demo

统计项目代码行数demo

作者: _String_ | 来源:发表于2018-02-01 20:28 被阅读0次

'''
package com.st.cms;
import java.io.;
import java.util.Scanner;
import java.util.regex.
;

import javax.xml.bind.ParseConversionEvent;
/**

  • 统计一个目录下(含子目录)所有java源文件中代码行数

  • @author ybsun

  • 20130304
    */
    public class CodeLins {
    private static int whiteLines;
    private static int commentLines;
    private static int normalLines;
    private static int fileounts;
    public static void CountLins(String CodePath){
    File folder = new File(CodePath);//要统计的源目录路径

    recur(folder);//递归该目录,统计其中代码行数
    
    System.out.println("总行数:" + (whiteLines+commentLines+normalLines));
    System.out.println("空行数: " + whiteLines);
    System.out.println("注释行数: " + commentLines);
    System.out.println("有效代码行数: " + normalLines);
    System.out.println("代码文件个数:"+fileounts);
    //return co;
    

    }
    /**

    • 递归方法 若源路径为目录,则递归,直至不包含子目录

    • @param file 源文件或目录
      /
      public static void recur(File file)
      {
      File[] files = file.listFiles();
      for(int i=0; i<files.length; i++)
      {
      //若源文件是目录,则递归
      if(files[i].isDirectory() == true)
      {
      recur(files[i]);
      }
      //若源文件是普通文件且为java源文件,则逐行分析之 匹配文件名时用到了正则表达式
      else if((files[i].isFile() == true) && files[i].getName().matches(".
      \.java$"))
      {
      fileounts++;
      parse(files[i]);
      }
      }
      }
      /**

    • 将java源文件逐行解析,统计每种代码数目 用到了正则表达式

    • @param file java源文件
      /
      public static void parse(File file)
      {
      BufferedReader br = null;
      try {
      br = new BufferedReader(new FileReader(file));
      String line = "";
      boolean isComment = false; //用来标记那些/
      ...*/注释不在同一行的情况

       while((line=br.readLine()) != null)
       {
           line = line.trim();//注:去除每行行首和行尾的空格  但不会删除换行符  区分开空白行与前面有缩进的代码行
           //if(line.matches("\\n[\\s| ]*\\r"))
           if(line.matches("^[\\s&&[^\\n]]*"))//空白行:以空白字符“ \t\n\x0B\f\r”开始且不含换行符
           {      //java中\n表示换行,\s匹配任意的空白符 包括换行符
      
               whiteLines++;
           }else if(line.startsWith("/*") && line.endsWith("*/"))//注释行:形如同一行中/*...*/
           {
               commentLines++;
           }else if(line.startsWith("/*") && !line.endsWith("*/"))//注释行:形如/*...
           {
               commentLines++;
               isComment = true;
           }else if(true == isComment)
           {
               commentLines++; //注释行:包含在/*和*/之间的注释行
               if(line.endsWith("*/"))
               {
                   isComment = false; //注释行: 形如 ...*/
               }
           }else if(line.startsWith("//"))
           {
               commentLines++; //注释行: 形如//....
           }else
           {
               normalLines++; //有效代码行
           }
       }
      

      } catch (FileNotFoundException e) {
      e.printStackTrace();
      } catch (IOException e) {
      e.printStackTrace();
      } finally
      {
      if(br != null)
      {
      try {
      br.close();
      br = null;
      } catch (IOException e) {
      e.printStackTrace();
      }

       }
      

      }
      }
      public static void main(String[] args){
      Scanner input = new Scanner(System.in);
      String dirPath = input.nextLine();
      CountLins(dirPath);
      }
      }
      '''

相关文章

  • 统计项目代码行数demo

    '''package com.st.cms;import java.io.;import java.util.Sc...

  • GIT统计代码量

    GIT统计代码量 Git统计个人提交代码行数 Git统计项目总行数 查看git上个人代码量(需要修改usernam...

  • 统计项目代码行数

    做开发这么久了,做项目这么久了。每天日出而作,日落而息。或加班加点,或通宵达旦赶进度。不知不觉中,每个类的代码行数...

  • 统计项目代码行数

    本文介绍了 3 种统计项目代码的方式,分别为 Cloc 库、VSCode 代码统计插件 —— VS Code Co...

  • 统计项目代码行数

  • 项目代码行数统计

    一、git命令统计 1、统计某人代码提交量 gitlog--author="mengfanxiao"--prett...

  • Git代码行数统计

    使用GitBash 1. 统计项目内所有代码行数 在代码路径下运行以下指令,可统计出当前仓库中的总代码行数: 输出...

  • Qt编写自定义控件69-代码行数统计

    一、前言 代码行数统计主要用来统计项目中的所有文件的代码行数,其中包括空行、注释行、代码行,可以指定过滤拓展名,比...

  • Qt开源作品10-代码统计组件

    一、前言 代码行数统计主要用来统计项目中的所有文件的代码行数,其中包括空行、注释行、代码行,可以指定过滤拓展名,比...

  • [code]统计项目代码量

    功能统计一个文件夹中所有指定后缀名文件中的数据行数 应用场景统计项目代码行数 代码 用例

网友评论

      本文标题:统计项目代码行数demo

      本文链接:https://www.haomeiwen.com/subject/gyjjzxtx.html