A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© rodgerun 中级黑马   /  2016-10-29 00:30  /  788 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本人写了一个想查询代码中空行数、注释行数和有效代码行数的小程序,但是程序实际运行的结果中,空行数总是比实际代码中空行数少一行,求大神解答?
import java.io.*;

public class TankCodeCounter {
         /*
          * 统计某文件中,所有java代码中空格、注释和有效代码的行数
         */
         static long totalWhiteLines = 0;  //总的空行数
        static long totalCommentLines = 0;  //总的注释行数
        static long totalNormalLines = 0;  //总的有效代码行数
        static long whiteLines = 0;  //单独某java文件的空格数
        static long commentLines = 0;  //单独某java文件的注释行数
        static long normalLines = 0;        //单独某java文件的有效代码行数
        
         public static void main (String[] args) {
                 File f = new File("E:\\share\\TankWarCodeCounter\\");  //文件的存放路径
                File[] codesFiles = f.listFiles();  //获取文件夹中的文件目录
               
                 for (File child : codesFiles) {  
                         if (child.getName().matches(".*\\.java$")) {  //找出文件中的java文件
                                parse(child);   //调用parse()方法,计算该java文件的空行数、注释行数以及有效代码行数
                                System.out.print(child.getName() + ": ");
                                 System.out.print("  whiteLines " + whiteLines);
                                 System.out.print(";  commentLines " + commentLines);
                                 System.out.println(";  normalLines " + normalLines);
                         }
                 }
                 System.out.println();
                 System.out.println("TotalWhiteLines: " + totalWhiteLines);
                 System.out.println("TotalCommentLines: " + totalCommentLines);
                 System.out.println("TotalNormalLines: " + totalNormalLines);
         }
         /*
          * parse方法用来统计每个java源代码中的空行数、注释行数以及有效代码数,
         * 并将文件夹中所有java文件的总空行数、总注释行数以及总有效代码行数分别
         * 存放在静态常量totalWhiteLines、totalCommentLines、totalNormalLines中
         */
         private static void parse(File f) {
                 BufferedReader br = null;  
                 Boolean comment = false;  //标注是否是注释行的辅助变量
                String lines = null;  //存放文件中读取的某一行
                whiteLines = 0;  
                 commentLines = 0;
                 normalLines = 0;
                 
                 try {
                         br = new BufferedReader(new FileReader(f));
                         while ((lines = br.readLine()) != null) {
                                 lines = lines.trim();  //去除每行中的空格数,防止注释代码中一空格开头而非/*或//开头
                                if (lines.matches("^[\\s&&[^\\n]]*$")) {  //统计代码中空格的行数
                                        whiteLines ++;
                                         totalWhiteLines ++;
                                 } else if (lines.startsWith("/*") && !lines.endsWith("*/")) {  //统计代码中多行注释的行数
                                        commentLines ++;
                                         totalCommentLines ++;
                                         comment = true;   
                                 } else if (true == comment) {  //comment=true时,表示改行为多行注释的其中一行
                                        commentLines ++;
                                         totalCommentLines ++;
                                         if (lines.endsWith("*/")) {
                                                 comment = false;
                                         }
                                 } else if (lines.startsWith("/*") && lines.endsWith("*/")) {  //以/*开口和一*/结尾的单行注释
                                        commentLines ++;
                                         totalCommentLines ++;
                                 } else if (lines.startsWith("//")) {  // 以“//”开头的单行注释
                                        commentLines ++;
                                         totalCommentLines ++;
                                 } else {   //除去空行和注释行,剩下的都为代码行
                                        normalLines ++;
                                         totalNormalLines ++;
                                 }
                         }
                 } catch (FileNotFoundException e) {
                         e.printStackTrace();
                 } catch (IOException e) {
                         e.printStackTrace();
                 } finally {
                         if (br != null) {
                                 try {
                                         br.close();
                                         br = null;
                                 } catch (IOException e) {
                                         e.printStackTrace();
                                 }
                         }
                 }
         }
}

附,好像是如果最后一行有空行的话,程序怎么都检测不到,这是为什么呢???

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马