java程式碼數統計工具

來源:互聯網
上載者:User

import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; /** * 程式碼數統計 * @author 霧非霧 * @date 2011-05-25 */ public class StatisticCodeLines { public static int normalLines = 0; //有效程式行數 public static int whiteLines = 0; //空白行數 public static int commentLines = 0; //注釋行數 public static void main(String[] args) throws IOException{ File file = new File("d://workspace//project"); if (file.exists()) { statistic(file); } System.out.println("總有效程式碼數: " + normalLines); System.out.println("總空白行數:" + whiteLines); System.out.println("總注釋行數:" + commentLines); System.out.println("總行數:" + (normalLines + whiteLines + commentLines)); } private static void statistic(File file) throws IOException { if (file.isDirectory()) { File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { statistic(files[i]); } } if (file.isFile()) { //統計副檔名為java的檔案 if(file.getName().matches(".*//.java")){ parse(file); } } } public static void parse(File file) { BufferedReader br = null; // 判斷此行是否為注釋行 boolean comment = false; int temp_whiteLines = 0; int temp_commentLines = 0; int temp_normalLines = 0; try { br = new BufferedReader(new FileReader(file)); String line = ""; while ((line = br.readLine()) != null) { line = line.trim(); if (line.matches("^[//s&&[^//n]]*$")) { // 空行 whiteLines++; temp_whiteLines++; } else if (line.startsWith("/*") && !line.endsWith("*/")) { // 判斷此行為"/*"開頭的注釋行 commentLines++; comment = true; } else if (comment == true && !line.endsWith("*/")) { // 為多行注釋中的一行(不是開頭和結尾) commentLines++; temp_commentLines++; } else if (comment == true && line.endsWith("*/")) { // 為多行注釋的結束行 commentLines++; temp_commentLines++; comment = false; } else if (line.startsWith("//")) { // 單行注釋行 commentLines++; temp_commentLines++; } else { // 正常程式碼 normalLines++; temp_normalLines++; } } System.out.println("有效行數" + temp_normalLines + " ,空白行數" + temp_whiteLines + " ,注釋行數" + temp_commentLines + " ,總行數" + (temp_normalLines + temp_whiteLines + temp_commentLines) + " " + file.getName()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); br = null; } catch (IOException e) { e.printStackTrace(); } } } } }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.