CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  J2EE / EJB / JMS

在线等:怎么写一程序读另一个java程序来得到这java程序中有多少注释行!

楼主lcld0408(Gogle)2004-09-04 16:41:10 在 Java / J2EE / EJB / JMS 提问

如题! 问题点数:100、回复次数:16Top

1 楼charlie0895(命----世上本没有路,我多走几次也就成了路.... )回复于 2004-09-04 16:49:22 得分 0

我感觉,首先你用的java文件要把那个被读的java文件当文件来读,这样通过判断可以取出,注释部分的内容,但是要通过jdk去编译的话,是不可能读到java文件中的注释部分Top

2 楼charlie0895(命----世上本没有路,我多走几次也就成了路.... )回复于 2004-09-04 16:52:30 得分 0

读文件,去取里面的字符,就可以找出关于注释的信息了Top

3 楼lcld0408(Gogle)回复于 2004-09-04 17:16:02 得分 0

可有具体的例子啊Top

4 楼allan1031(加西亚想睡觉)回复于 2004-09-04 19:29:09 得分 0

全文扫描并用正则和堆栈(标志位)处理  
   
  用行做读取  
   
  当发现一个   //   或者   /*标签匹配的时候,将该标签记数再压入堆栈---但是//不用再压缩。  
   
  并保证该堆栈只记载一个元素  
   
  当再次扫描出现//   或者/*时候,如果堆栈中已有元素,则不需要理会  
   
  如果扫描到*/且堆栈中有元素,则将元素出栈Top

5 楼flyingbug(Effective Refactoring)回复于 2004-09-04 21:55:22 得分 0

楼上正解Top

6 楼zez(思恩 闭关练功ing...)回复于 2004-09-05 13:44:40 得分 0

前两天写了一个,可以统计  
  注释行,空行,总行,代码行,既有代码又有注释行,注释中空行...  
  不过代码不在手里.  
  要的话等我去公司给你Top

7 楼kennyworld()回复于 2004-09-05 14:25:29 得分 0

以前知道用元素匹配法是一个不错的方法,但是在程序实现中觉得记录和实现匹配有一定的难度。楼上用堆栈实现确实是个不错的选择。Top

8 楼charlie0895(命----世上本没有路,我多走几次也就成了路.... )回复于 2004-09-06 08:59:11 得分 0

其实,怎么读文件,怎么去计算都不是问题呀,这读的是java文件和其他文件也都没有区别的,也就没有意义了Top

9 楼zez(思恩 闭关练功ing...)回复于 2004-09-07 12:45:06 得分 100

package   com.huawei.sw1.clog;  
   
  import   java.io.File;  
  import   java.io.FileReader;  
  import   java.io.LineNumberReader;  
  import   java.io.IOException;  
   
  /**  
    *   @author   zez  
    *    
    *   源文件代码行统计工具Java版   :)  
    */  
  public   class   Clog   {  
   
  //多文件代码统计汇总结果  
  private   int   gcommentLineNum   =   0;  
   
  private   int   gcodeAndCommentLinNum   =   0;  
   
  private   int   gblankLineNum   =   0;  
   
  private   int   gtotalLineNum   =   0;  
   
  private   int   gblankAndCommentLinNum   =   0;  
   
   
  //单个文件统计信息  
  private   LineNumberReader   lReader;  
   
  private   int   commentLineNum   =   0;  
   
  private   int   codeAndCommentLinNum   =   0;  
   
  private   int   blankLineNum   =   0;  
   
  private   int   totalLineNum   =   0;  
   
  private   int   blankAndCommentLinNum   =   0;  
   
  // private   int   logicLineNum   =   0;  
   
  //初始化  
  private   void   init()   {  
  lReader   =   null;  
  commentLineNum   =   0;  
  codeAndCommentLinNum   =   0;  
  blankLineNum   =   0;  
  totalLineNum   =   0;  
  blankAndCommentLinNum   =   0;  
  // logicLineNum   =   0;  
  }  
   
  /**  
    *   判断文件类型,并取得此文件  
    *    
    *   @param   fileName  
    *   @return   file,   if   not   a   source   code   file,   return   NULL   .  
    */  
  private   File   getSourceFile(String   fileName)   {  
  if   (fileName   ==   null   ||   fileName.trim().length()   ==   0)   {  
  System.out.println("\nThe   file   name   /*   is   null   !\n");  
  return   null;  
  }  
  File   file   =   new   File(fileName);  
  // 如果是目录,返回此目录  
  if   (file.isDirectory())   {  
  return   file;  
  }  
  //文件是否存在  
  if   (!file.exists())   {  
  System.out.println("\nThe   file   "   +   fileName  
  +   "   is   don't   exists   !\n");  
  return   null;  
  }  
   
  /**  
    *   判断是否是.c   .cpp   .java文件  
    */  
  if   (fileName.indexOf('.')   >   0)   {  
  String   fileExt   =   fileName.substring(fileName.lastIndexOf('.')   +   1).toLowerCase();  
  if   (!fileExt.equals("c")   &&   !fileExt.equals("cpp")   &&   !fileExt.equals("h")  
  &&   !fileExt.equals("java")&&   !fileExt.equals("hpp"))   {  
   
  System.out.println("\nThe   file   "   +   fileName  
  +   "   is   not   a   C   or   C++   or   JAVA   source   file   !\n");  
  return   null;  
   
  }  
  }  
   
  //文件大小是否大于64k,不过大于64k仍可以计算  
  if   (file.length()   >   65535)   {  
  System.out.println("\nThe   file   "   +   fileName  
  +   "   is   too   large   than   64k   ,but   It   can   work   :)!\n");  
  }  
  //文件大小如果小于3个字节,返回空  
  if   (file.length()   <   3)   {  
  System.out.println("\nThe   file   "   +   fileName  
  +   "   has   no   content   \n");  
  return   null;  
  }  
   
  return   file;  
  }  
   
  /**  
    *   打开文件  
    *   @param   file    
    *   @throws   Exception  
    */  
  private   void   openFile(File   file)   throws   Exception   {  
  try   {  
  lReader   =   new   LineNumberReader(new   FileReader(file));  
  }   catch   (Exception   e)   {  
  throw   e;  
  }  
  }  
   
  /***************************************************************************  
    *   行数计算主函数   算法:   循环每次读取一行,分几种情况进行计算   1.空行   2.//开头   肯定为注释行   3.//在代码后面,  
    *   按代码行算,并处理//在字符串中情况   4.以/*开头的情况,调用专门块注释计算方法   5./*   在代码后面情况,处理同上  
    *      
    */  
   
  private   void   countLineNum()   throws   Exception   {  
   
  try   {  
  while   (true)   {   //未到文件尾  
  String   str   =   lReader.readLine();   //取得一行代码  
   
  totalLineNum++;   //总行数加一  
   
  if   (str   ==   null)   {   //判断是否为文件尾  
   
  totalLineNum--;  
  return;  
  }   else   {   //否则进行计算  
  str   =   str.trim();   //去两头空格  
  // countLogicLineNum(str);  
  if   (str.length()   ==   0)   {   //如果为空,则为空行,空行数加一  
  blankLineNum++;  
   
  }   else   if   (str.startsWith("//"))   {   //如果是以//开头,为注释行,即使此行有//注释,但非开头,则要按代码行算  
  commentLineNum++;  
   
  }   else   if   (str.indexOf("//")   >   0   &&   isNotInStr(str,   "//"))   {   //   //在行中,并判断不是在字符串中  
  codeAndCommentLinNum++;  
  }   else   if   (str.indexOf("/*")   >=   0)   {  
  //如果/*在行中,判断是否是在""内   ,否则为注释行  
  if   (isNotInStr(str,   "/*"))   {  
        countCommentLin(str);//计算/**/   内的注释行数  
  }  
  }  
  }  
  }  
  }   catch   (IOException   e)   {  
  throw   new   Exception("文件读取时出错   !\n");  
  }  
  }  
   
  /**Top

10 楼zez(思恩 闭关练功ing...)回复于 2004-09-07 12:45:17 得分 0

 
    *   块注释计算方法   判断是否块注释结束   否则读下一行   循环  
    */  
  private   void   countCommentLin(String   str)   throws   Exception   {  
  try   {  
  commentLineNum++;  
  //处理   /*some   comment   */   即只一行的情况,并处理   /*/   这种情况  
                          if((str.substring(   str.indexOf(   "/*")+2).indexOf(   "*/")>=0)){  
                          //是否有代码  
  if(str.length()   >   str.indexOf(   "*/")-str.indexOf(   "/*")){  
  codeAndCommentLinNum++;  
      commentLineNum--;  
      // countLogicLineNum(str);  
  }  
  //处理   /*xxx*/   some   code   /*     这种情况  
  if(str.lastIndexOf(   "*/")>str.lastIndexOf(   "/*")+1)  
  {  
  // countLogicLineNum(str);  
  return;  
  }  
  }  
                          //一般情况,   some   code   /*   some   comment  
                          if(str.indexOf(   "/*")>0){  
  codeAndCommentLinNum++;  
      commentLineNum--;  
          }  
   
                do   {  
  str   =   lReader.readLine();   //不是注释尾,取下一行代码  
  totalLineNum++;  
  //如果到文件尾,返回  
  if   (str   ==   null)   {  
  totalLineNum--;  
  return;  
  }  
  str   =   str.trim();   //去空格  
  //是否到注释尾,如果后面还有代码,按混合行算  
  if   (str.indexOf("*/")   >=   0)   {  
  commentLineNum++;  
  if   (str.length()   >   str.indexOf("*/")   +   2)   {  
  codeAndCommentLinNum++;  
  commentLineNum--;  
  // countLogicLineNum(str);  
  }  
  return;  
  }   else   {  
  if   (str.length()   ==   0)   {   //是空行  
  blankLineNum++;   //空行加一  
  blankAndCommentLinNum++;   //注释中空行加一  
                                          }   else   {  
  commentLineNum++;   //注释加一  
  }  
  }  
  }   while   (true);  
   
  }   catch   (IOException   e)   {  
  throw   new   Exception("文件读取时出错   !\n");  
  }  
  }  
   
  /*  
    *   判断   某字符   是否是字符串中,特别注释字符.   算法   :   字符串中是否有注释符号   如没有,返回   false   字符串中有   "    
    *   ,如没有,返回true   注释符号在""之前,返回true;否则,继续   str   =   str的非\"的"后面部分   循环  
    */  
  private   boolean   isNotInStr(String   str,   String   subStr)   {  
   
  while   (str.indexOf(subStr)   >=   0)   {  
  if   (str.indexOf('"')   >=   0)   {  
  if   (str.indexOf('"')   >   str.indexOf(subStr))   {  
  return   true;  
  }   else   {  
  str   =   str.substring(str.indexOf('"')   +   1);  
   
  while   (str.indexOf('\\')   >=   0  
  &&   str.indexOf('"')   ==   str.indexOf('\\')   +   1)   {  
  str   =   str.substring(str.indexOf('"')   +   1);  
  }  
  str   =   str.substring(str.indexOf('"')   +   1);  
  }  
  }   else   {  
  return   true;  
  }  
  }  
  return   false;  
  }  
  /**  
    *   计算一行中的逻辑代码行  
    *   基本法:计算代码行中的   ;   和   {   的个数  
    *   缺陷:   现在无法对注释中的   ;   和   {   情况进行剔除   (//后面的注释和   块注释尾标志所在行的注释)  
    *   有兴趣的话来完成吧:)  
    *   做完了最好给我发一份过来   .OK  
    *   @param   str  
    */  
  /* private   void   countLogicLineNum(String   str)  
  {  
      String   aStr   =   str;  
      while(aStr.indexOf(   ";")>0   &&   isNotInStr(aStr,";"))  
      {  
      logicLineNum++;  
      aStr   =   aStr.substring(   aStr.indexOf(   ";")+1);  
      }  
      aStr   =   str;  
      while(aStr.indexOf(   "{")>=0)  
      {  
      logicLineNum++;  
      aStr   =   aStr.substring(   aStr.indexOf(   "{")+1);  
      }  
       
  }**/  
  /**  
    *   打印计算结果  
    *  
    */  
  private   void   printResult()   {  
   
  int   codeLineNum   =   totalLineNum   -   blankLineNum   -   commentLineNum;  
  System.out.println("总行数为             :   "   +   totalLineNum);  
  System.out.println("代码行数为         :   "   +   codeLineNum);  
  System.out.println("总注释行数为     :   "  
  +   (commentLineNum   +   codeAndCommentLinNum));  
  System.out.println("空行数为             :   "   +   blankLineNum);  
  System.out.println("混合行数为         :   "   +   codeAndCommentLinNum);  
  System.out.println("纯注释行数为     :   "   +   commentLineNum);  
  System.out.println("注释中空行数为:   "   +   blankAndCommentLinNum);  
  // System.out.println("逻辑代码行数为   :   "   +   logicLineNum);  
  int   commentRat   ;  
  if((commentLineNum   +   codeAndCommentLinNum)==0   ||   totalLineNum==0   )  
  {  
  commentRat   =   0;  
  }else{  
  commentRat   =   (100   *   (commentLineNum   +   codeAndCommentLinNum)   /   totalLineNum);  
  }  
  System.out.println("注释率为           :   "  
  +   commentRat  
  +   "%");  
  }  
   
  /**  
    *   打印计算结果  
    *  
    */  
  private   void   printTotalResult()   {  
   
  int   gcodeLineNum   =   gtotalLineNum   -   gblankLineNum   -   gcommentLineNum;  
  System.out.println("总行数为             :   "   +   gtotalLineNum);  
  System.out.println("代码行数为         :   "   +   gcodeLineNum);  
  System.out.println("总注释行数为     :   "  
  +   (gcommentLineNum   +   gcodeAndCommentLinNum));  
  System.out.println("空行数为             :   "   +   gblankLineNum);  
  System.out.println("混合行数为         :   "   +   gcodeAndCommentLinNum);  
  System.out.println("纯注释行数为     :   "   +   gcommentLineNum);  
  System.out.println("注释中空行数为:   "   +   gblankAndCommentLinNum);  
  // System.out.println("逻辑代码行数为   :   "   +   logicLineNum);  
  int   commentRat   ;  
  if((gcommentLineNum   +   gcodeAndCommentLinNum)==0   ||   gtotalLineNum==0   )  
  {  
  commentRat   =   0;  
  }else{  
  commentRat   =   (100   *   (gcommentLineNum   +   gcodeAndCommentLinNum)   /   gtotalLineNum);  
  }  
  System.out.println("注释率为           :   "   +   commentRat+   "%");  
  }  
  /**  
    *   统计结果汇总  
    *  
    */  
  private   void   countTotalNum(){  
  gcommentLineNum   +=   commentLineNum   ;  
  gcodeAndCommentLinNum   +=   codeAndCommentLinNum;  
  gblankLineNum   +=   blankLineNum;  
  gtotalLineNum   +=   totalLineNum;  
  gblankAndCommentLinNum   +=   blankAndCommentLinNum;  
  }  
  /**  
    *   嵌套计算文件夹内所有文件  
    *   @param   fileName   //文件或目录名  
    *   @throws   Exception  
    */  
  private   void   count(String   fileName)   throws   Exception   {  
  try   {  
  File   file   =   getSourceFile(fileName);  
  if   (null   !=   file)   {  
  if   (file.isFile())   {  
  System.out.println("\n源文件:   "   +   fileName   +   "   统计信息为:   ");  
  init();  
  openFile(file);  
  countLineNum();  
  printResult();  
  countTotalNum();  
  }   else   if   (file.isDirectory())   {  
  String[]   fNameList   =   file.list();  
  for   (int   i   =   0;   i   <   fNameList.length;   i++)   {  
  count(fileName   +   "\\"   +   fNameList[i]);  
  }  
          }  
  }  
  }   catch   (Exception   e)   {  
  throw   e;  
  }  
  }  
   
  public   static   void   main(String[]   args)   {  
  try   {  
  String   fileName;  
  if   (args   ==   null   ||   args.length   ==   0)   {  
  System.out.println("请输入要统计的文件或目录:\n");  
  byte[]   buff   =   new   byte[255];  
  System.in.read(buff);  
  fileName   =   new   String(buff).trim();  
   
  }   else   {  
  fileName   =   args[0];  
  }  
  //     fileName   =   "c:\\test";  
   
  Clog   clog   =   new   Clog();  
  long   begin   =   System.currentTimeMillis()   ;  
  clog.count(fileName);  
  if(new   File(fileName).isDirectory()   ){  
        System.out.println("目录   "+fileName+"   下所有源文件统计汇总如下:\n")   ;  
        clog.printTotalResult()   ;  
  }  
  long   end   =   System.currentTimeMillis()   ;  
          System.out.println("\n   用时:   "+(end-begin)+"   毫秒   .")   ;  
  }   catch   (Exception   e)   {  
                          if(null==e.getMessage()   ||   e.getMessage().trim()   .length()   ==0){  
                          System.out.println("   系统出错,错误信息为   :\n")   ;  
                          e.printStackTrace()   ;  
                          }else{  
                          System.out.println("   程序出错,错误信息为   :\n       "   +   e.getMessage());  
                          }  
  }  
  }  
  }Top

11 楼guojiafuzhuxi(炒股炒成股东)回复于 2004-09-07 13:21:41 得分 0

楼上是强人。Top

12 楼wantsong(WantSong)回复于 2004-09-07 13:24:00 得分 0

强。  
  mark.Top

13 楼dongdongmaster(冬冬)回复于 2004-09-07 15:44:06 得分 0

强人Top

14 楼sunboyyq(yyq)回复于 2004-09-07 16:06:22 得分 0

收藏之,学习.....Top

15 楼allan1031(加西亚想睡觉)回复于 2004-09-10 09:35:16 得分 0

五体投地~~~拜服Top

16 楼sswt(sweater)回复于 2004-09-10 09:41:16 得分 0

基本功很扎实嘛:)Top

相关问题

  • 如何用Java程序删除sql脚本中的注释
  • 急求,程序注释
  • linux下的java程序,到windows下看注释部分是乱码???
  • 高分求助,那位有用java多线程编的并行计算的程序(至少300行代码,有注释)。
  • 高分(100)求 电影 WEB 在线播放源程序及注释说明。
  • 怎样注释多行ASP源程序?
  • 注释会影响程序吗?/*“*”*/
  • 有关程序加注释问题!
  • 帮给加点注释,程序很短
  • 程序函数注释讨论!

关键词

  • 文件
  • 代码
  • 注释
  • 堆栈
  • 程序
  • codeandcommentlinnum
  • commentlinenum
  • 元素
  • 匹配
  • indexof

得分解答快速导航

  • 帖主:lcld0408
  • zez

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo