zoukankan      html  css  js  c++  java
  • (原创)一个改自java的代码统计工具

      1using System;
      2using System.IO;
      3
      4namespace CodeTell{
      5    /// <summary>
      6    ///  CodeTell 的摘要说明。
      7    /// </summary>

      8
      9    class  CodeTell
     10    {
     11        private int codeLines = 0, commentLines = 0, fileCount = 0, floderCount = 0,characterCount = 0;
     12        private long totalFileLength = 0;
     13        /// <summary>
     14        /// 应用程序的主入口点。
     15        /// </summary>

     16        [STAThread]
     17        static void Main(string[] args)
     18        {
     19            //
     20            // TODO: 在此处添加代码以启动应用程序
     21            //
     22            (new CodeTell()).Start();
     23            
     24        }

     25
     26        public void Start()
     27        {
     28            DirectoryInfo folder = new DirectoryInfo(".");
     29
     30            examineList(folder);
     31
     32            //输出信息
     33            Console.WriteLine("Folder:\t\t{0}",floderCount);
     34            Console.WriteLine("Files:\t\t{0}",fileCount);
     35            Console.WriteLine("Comment Lines:\t{0}",commentLines);
     36            Console.WriteLine("Code Lines:\t{0}",codeLines);
     37            Console.WriteLine("Char Count:\t{0}",characterCount);
     38            Console.WriteLine("Files Length:\t{0}",totalFileLength);
     39        
     40            Console.Read();
     41        }

     42
     43        protected void examineList(DirectoryInfo dir)
     44        {
     45            FileInfo[] finfo = dir.GetFiles();
     46            DirectoryInfo[] dinfo = dir.GetDirectories();
     47        
     48            floderCount += dinfo.Length;
     49
     50            for(int i = 0; i<finfo.Length; i++)
     51            {
     52                calcFile(finfo[i]);
     53            }

     54            //遍历目录
     55            for(int i = 0; i< dinfo.Length; i++)
     56            {
     57                examineList(dinfo[i]);
     58            }

     59        }

     60
     61
     62        private void calcFile(FileInfo file)
     63        {
     64            StreamReader sreader;
     65            string fname,line;
     66            bool insideComment;
     67
     68            fname = file.FullName;
     69
     70            if(fname == null)
     71            {
     72                Console.WriteLine("Somehow the file was null");
     73            }

     74                //对.cs文件进行分析
     75            else if(fname.ToLower().EndsWith(".cs"))
     76            {
     77                totalFileLength += file.Length;
     78                fileCount++;
     79
     80                try
     81                {
     82                    sreader = new StreamReader(fname);
     83                    Console.WriteLine("    {0}",fname);
     84                    line = sreader.ReadLine();
     85                    if( line != null)
     86                        line = line.Trim();
     87                    insideComment = false;
     88                    int temp;
     89                    while(line != null)
     90                    {
     91                        characterCount += line.Length;
     92                        if(line.Length == 0)
     93                        {
     94                            if((temp = line.IndexOf("*/")) >= 0)
     95                            {
     96                                insideComment = false;
     97                                if(line.Length > temp)
     98                                    codeLines++;
     99                                if(temp > 2)
    100                                    commentLines++;
    101                            }

    102                        }

    103                        else
    104                        {
    105                            if(!line.StartsWith("//"))
    106                                codeLines++;
    107                            if(line.StartsWith("/*"))
    108                                insideComment = true;
    109                            if(line.IndexOf("//")>=0)
    110                                commentLines++;
    111                        }

    112
    113                        line = sreader.ReadLine();
    114                        if(line != null)
    115                            line = line.Trim();
    116
    117                    }
    //end while
    118                }

    119                catch(Exception e)
    120                {
    121                    Console.WriteLine(e.Message);
    122                }

    123            }

    124        }

    125
    126    }

    127}

    128
  • 相关阅读:
    如何使用SAP Intelligent Robotic Process Automation自动操作Excel
    OpenSAML 使用引导 IV: 安全特性
    Spring Cloud Zuul 网关使用与 OAuth2.0 认证授权服务
    微服务架构集大成者—Spring Cloud (转载)
    Spring Cloud Eureka 服务注册列表显示 IP 配置问题
    使用 Notification API 开启浏览器桌面提醒
    SignalR 中使用 MessagePack 序列化提高 WebSocket 通信性能
    配置 Nginx 的目录浏览功能
    关于 Nginx 配置 WebSocket 400 问题
    Migrate from ASP.NET Core 2.0 to 2.1
  • 原文地址:https://www.cnblogs.com/CSharp/p/345993.html
Copyright © 2011-2022 走看看