java代碼行數統計工具類


  1 package com.syl.demo.test;
  2 
  3 import java.io.*;
  4 
  5 /**
  6  * java代碼行數統計工具類
  7  * Created by 孫義朗 on 2017/11/17 0017.
  8  */
  9 public class CountCodeLineUtil {
 10     private static int normalLines = 0;  //有效程序行數
 11     private static int whiteLines = 0;   //空白行數
 12     private static int commentLines = 0; //注釋行數
 13 
 14     public static void countCodeLine(File file) {
 15         System.out.println("代碼行數統計:" + file.getAbsolutePath());
 16         if (file.exists()) {
 17             try {
 18                 scanFile(file);
 19             } catch (IOException e) {
 20                 e.printStackTrace();
 21             }
 22         } else {
 23             System.out.println("文件不存在!");
 24             System.exit(0);
 25         }
 26         System.out.println(file.getAbsolutePath() + " ,java文件統計:" +
 27                 "總有效代碼行數: " + normalLines +
 28                 " ,總空白行數:" + whiteLines +
 29                 " ,總注釋行數:" + commentLines +
 30                 " ,總行數:" + (normalLines + whiteLines + commentLines));
 31     }
 32 
 33     private static void scanFile(File file) throws IOException {
 34         if (file.isDirectory()) {
 35             File[] files = file.listFiles();
 36             for (int i = 0; i < files.length; i++) {
 37                 scanFile(files[i]);
 38             }
 39         }
 40         if (file.isFile()) {
 41             if (file.getName().endsWith(".java")) {
 42                 count(file);
 43             }
 44         }
 45     }
 46 
 47     private static void count(File file) {
 48         BufferedReader br = null;
 49         // 判斷此行是否為注釋行
 50         boolean comment = false;
 51         int temp_whiteLines = 0;
 52         int temp_commentLines = 0;
 53         int temp_normalLines = 0;
 54 
 55         try {
 56             br = new BufferedReader(new FileReader(file));
 57             String line = "";
 58             while ((line = br.readLine()) != null) {
 59                 line = line.trim();
 60                 if (line.matches("^[//s&&[^//n]]*$")) {
 61                     // 空行
 62                     whiteLines++;
 63                     temp_whiteLines++;
 64                 } else if (line.startsWith("/*") && !line.endsWith("*/")) {
 65                     // 判斷此行為"/*"開頭的注釋行
 66                     commentLines++;
 67                     comment = true;
 68                 } else if (comment == true && !line.endsWith("*/")) {
 69                     // 為多行注釋中的一行(不是開頭和結尾)
 70                     commentLines++;
 71                     temp_commentLines++;
 72                 } else if (comment == true && line.endsWith("*/")) {
 73                     // 為多行注釋的結束行
 74                     commentLines++;
 75                     temp_commentLines++;
 76                     comment = false;
 77                 } else if (line.startsWith("//")) {
 78                     // 單行注釋行
 79                     commentLines++;
 80                     temp_commentLines++;
 81                 } else {
 82                     // 正常代碼行
 83                     normalLines++;
 84                     temp_normalLines++;
 85                 }
 86             }
 87 
 88             System.out.println(file.getName() +
 89                     " ,有效行數" + temp_normalLines +
 90                     " ,空白行數" + temp_whiteLines +
 91                     " ,注釋行數" + temp_commentLines +
 92                     " ,總行數" + (temp_normalLines + temp_whiteLines + temp_commentLines));
 93         } catch (FileNotFoundException e) {
 94             e.printStackTrace();
 95         } catch (IOException e) {
 96             e.printStackTrace();
 97         } finally {
 98             if (br != null) {
 99                 try {
100                     br.close();
101                     br = null;
102                 } catch (IOException e) {
103                     e.printStackTrace();
104                 }
105             }
106         }
107     }
108 
109     //測試
110     public static void main(String[] args) {
111         File file = new File("F:\\myweb");
112         countCodeLine(file);
113     }
114 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM