代码以实现统计单独文件和总工程代码的行数,现想要分别统计 工程中JAVA 的代码行数和JSP的代码行数,通过System.out.println("请输入要统计的文件或目录:\n");  
byte[]   buff   =   new   byte[255];   
System.in.read(buff);  
System.out.println("请输入要统计的文件类型:\n");
byte[]   buff1   =   new   byte[255];   
System.in.read(buff1);  
fileName   =   new   String(buff).trim();   
    fileExt = new   String(buff1).trim();   
输入地址和类型(*.JSP或者*.JAVA)  总体代码如下import java.io.File;   
import java.io.FileOutputStream;
import java.io.FileReader;   
import java.io.LineNumberReader;   
import java.io.IOException;   
import java.io.PrintStream;    
/**   
   *   @author  wj
   *     
   *   源文件代码行统计工具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 void init()
  {   
    lReader = null;   
    commentLineNum = 0;   
    codeAndCommentLinNum = 0;   
    blankLineNum = 0;   
    totalLineNum = 0;   
    blankAndCommentLinNum = 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;   
       }   
    
      /**   
        *   判断是否是.jsp   .java文件   
       */   
      if   (fileName.indexOf('.')>0){   
      String fileExt = fileName.substring(fileName.lastIndexOf('.')+1).toLowerCase();  //    文件大小是否大于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;   
      }   
      
      
      if   (fileExt.equalsIgnoreCase(fileExt))
      {
       return file;
      } 
    
  }   
     return   null;   
  }   
    
/**   
        *   打开文件   
        *   @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();   //去两头空格   
  
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");   
  }   
  }   

解决方案 »

  1.   

    /*   
       *   判断   某字符   是否是字符串中,特别注释字符.   算法   :   字符串中是否有注释符号   如没有,返回   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;   
      }   
     
     
       
      /**   
       *   打印计算结果   
       *   
       */   
      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);   
        
      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);   
       
      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;   
      }
      

  2.   

    /**   
       *   嵌套计算文件夹内所有文件   
       *   @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 ;   
    String   fileExt;
    if   (args   ==   null   ||   args.length   ==   0)   {   
       System.out.println("请输入要统计的文件或目录:\n");  
    byte[]   buff   =   new   byte[255];   
    System.in.read(buff);  
    System.out.println("请输入要统计的文件类型:\n");
    byte[]   buff1   =   new   byte[255];   
    System.in.read(buff1);  
    fileName   =   new   String(buff).trim();   
        fileExt = new   String(buff1).trim();  }   else   {   
    fileName   =   args[0]; 
    fileExt  =  args[0];
    }        
         Clog   clog   =   new   Clog();   
         long   begin   =   System.currentTimeMillis() ; 
    //     PrintStream ps=new PrintStream(new FileOutputStream("temp2.txt"));
    //     System.setOut(ps);
    //     System.setErr(ps);
         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());   
              }   
    }   
      }   
    }
      

  3.   

    问题主要卡在  if   (fileExt.equalsIgnoreCase(fileExt))
          {
           return file;
          } 
      怎么去定义类型
      

  4.   

    使用 java.io.FileFilter,重载一下 accept 方法。
      

  5.   

    JSP 的注释 <%-- -->、<!-- --> 好像没有被统计。
      

  6.   

    注释率计算方法不正确,全是 int 型的不能这样计算,改成commentRat = (100F * (gcommentLineNum + gcodeAndCommentLinNum) / gtotalLineNum);System.out.printf("注释率为: %.2f%%%n", commentRat);
      

  7.   

    bao110908(长牙了,好痛)  能给点具体代码吗谢谢
    JSP 的注释 <%-- -->、<!-- -->  是没有,现在主要是功能实现