首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

送分帖解决思路

2012-01-03 
送分帖代码以实现统计单独文件和总工程代码的行数,现想要分别统计工程中JAVA的代码行数和JSP的代码行数,通

送分帖
代码以实现统计单独文件和总工程代码的行数,现想要分别统计   工程中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 ");      
  }      
  }      


[解决办法]
收藏...
[解决办法]
使用 java.io.FileFilter,重载一下 accept 方法。
------解决方案--------------------


jf
[解决办法]
mark
[解决办法]
把偶头看晕了-^-

热点排行