JavaRegex(三)、代碼量統計工具(統計java源檔案中注釋、代碼、空白行數量)

來源:互聯網
上載者:User

       比如想統計一個Java程式員一天寫代碼的工作量(如:有效代碼多少行、空行多少、注釋多少行等),這個小工具也許能做為一個參考的依據。

    思路:因為每個java源檔案的內容基本包括java語句、空白行、注釋三部份組成(不包括註解),所以要統計某個檔案這三部份的內容各佔多少時,只需寫三個匹配這幾部份內容的Regex即可。然後通過IO流讀取檔案中的每一行,並根據正則匹配的結果,累加每部份匹配的數量即可。

    注釋行:單行注釋(//)、多行注釋、文檔注釋。正則:((//)|(/\\*+)|((^\\s)*\\*)|((^\\s)*\\*+/))+

    空白行:一行內只有空格、\t、\n等非可視字元表示空白行。正則:^\\s*$

    程式碼:以分號“;“結束為一行有效代碼。正則:(?!import|package).+;\\s*(((//)|(/\\*+)).*)*

    實現方式:

        1、使用者在控制台輸入要統計的java檔案路徑或目錄

        2、根據路徑建立一個File對象,判斷檔案是否有效,並給出提示。最後將檔案和IO流綁定

        3、迴圈讀取檔案中的某一行,並根據正則匹配的結果累記每部份匹配的數量


完整代碼如下:
package regex;import java.io.BufferedReader;import java.io.File;import java.io.FileFilter;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;import java.util.Scanner;import java.util.regex.Pattern;/** * 代碼統計工具 * 統計某個java源檔案或某個目錄中所有java源檔案中注釋、空白行、程式碼各佔多少行 * 注釋:包括單行注釋(//)、多行注釋、文檔注釋 * 空白行:一行內沒有任何內容的表示空白行 * 程式碼:以分號“;”結束的一條語句,可以統計為一行有效代碼 */public class CodeStatDemo {// 記錄注釋行數static long annotationLine = 0;// 記錄空白行數static long blankLine = 0;// 記錄有效代碼的行數static long codeLine = 0;// 代碼總行數static long totalLine = 0;// 檔案總數static long fileCount = 0;public static void main(String[] args) throws FileNotFoundException  {System.out.println("請輸入要統計代碼量的java檔案或java目錄:");Scanner in = new Scanner(System.in);String filePath = in.nextLine();File file = new File(filePath);// 根據使用者輸入的檔案名稱和目錄執行代碼量統計codeStat(file);System.out.println("----------統計結果---------");System.out.println("檔案數量:" + fileCount + "個");System.out.println(file + "檔案/目錄總行數:" + totalLine);System.out.println("程式碼數:" + codeLine);System.out.println("注釋行數:" + annotationLine);System.out.println("空白行數:" + blankLine);long otherLine = totalLine - (codeLine + annotationLine + blankLine);System.out.println("其它行數:" + otherLine);}private static void codeStat(File file) throws FileNotFoundException {if (file == null || !file.exists()) throw new FileNotFoundException(file + ",檔案不存在!");fileCount ++;// 檔案數累加if (file.isDirectory()) {File[] files = file.listFiles(new FileFilter() {@Overridepublic boolean accept(File pathname) {return pathname.getName().endsWith(".java") || pathname.isDirectory();}});for (File target : files) {codeStat(target);}} else {BufferedReader bufr = null;try {// 將指定路徑的檔案與字元流綁定bufr = new BufferedReader(new InputStreamReader(new FileInputStream(file)));} catch (FileNotFoundException e) {throw new FileNotFoundException(file + ",檔案不存在!" + e);}// 定義匹配每一行的正則匹配器Pattern annotationLinePattern = Pattern.compile("((//)|(/\\*+)|((^\\s)*\\*)|((^\\s)*\\*+/))+", Pattern.MULTILINE + Pattern.DOTALL);// 注釋匹配器(匹配單行、多行、文檔注釋)Pattern blankLinePattern = Pattern.compile("^\\s*$");// 空白行匹配器(匹配斷行符號、tab鍵、空格)Pattern codeLinePattern = Pattern.compile("(?!import|package).+;\\s*(((//)|(/\\*+)).*)*",Pattern.MULTILINE + Pattern.DOTALL); // 程式碼匹配器(以分號結束為一行有效語句,但不包括import和package語句)// 遍曆檔案中的每一行,並根據正則匹配的結果記錄每一行匹配的結果String line = null;try {while((line = bufr.readLine()) != null) {if (annotationLinePattern.matcher(line).find()) {annotationLine ++;}if (blankLinePattern.matcher(line).find()) {blankLine ++;}if (codeLinePattern.matcher(line).matches()) {codeLine ++;} totalLine ++;}} catch (IOException e) {throw new RuntimeException("讀取檔案失敗!" + e);} finally {try {bufr.close();// 關閉檔案輸入資料流並釋放系統資源} catch (IOException e) {throw new RuntimeException("關閉檔案輸入資料流失敗!");}}}}}

測試結果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.