Java 程式碼統計

來源:互聯網
上載者:User

標籤:log   ace   程式碼數   檔案夾路徑   查詢   getname   util   bool   normal   

package codecounter;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.regex.Matcher;import java.util.regex.Pattern;public class CodeCounter {    static long commentLine = 0;      static long whiteLine = 0;      static long normalLine = 0;      static long totalLine = 0;      static boolean comment = false;      static final String base_dir = "D:\\javap\\qdgs\\qdgs_gzfw\\src\\java\\qdgsgzfw";      public static void main(String[] args) {          File file = new File(base_dir); // 在這裡輸入需要統計的檔案夾路徑                  getChild(file);                 System.out.println("有效程式碼數: " + normalLine);          System.out.println("注釋行數: " + commentLine);          System.out.println("空白行數: " + whiteLine);          System.out.println("總程式碼數: " + totalLine);      }        private static void getChild(File child) { // 遍曆子目錄          if (child.getName().matches(".*\\.java$")) { // 只查詢java檔案              try {                  BufferedReader br =  new BufferedReader(new FileReader(child));                  String line = "";                  while ((line = br.readLine()) != null) {                      parse(line);                  }              } catch (FileNotFoundException e1) {                  e1.printStackTrace();              } catch (IOException e) {                  e.printStackTrace();              }          }          if (child.listFiles() != null) {              for (File f : child.listFiles()) {                  getChild(f);              }          }      }        private static void parse(String line) {          line = line.trim();          totalLine++;          if (line.length() == 0) {              whiteLine++;          } else if (comment) {              commentLine++;              if (line.endsWith("*/")) {                  comment = false;              } else if (line.matches(".*\\*/.+")) {                  normalLine++;                  comment = false;              }          } else if (line.startsWith("//")) {              commentLine++;          } else if (line.matches(".+//.*")) {              commentLine++;              normalLine++;          } else if (line.startsWith("/*") && line.matches(".+\\*/.+")) {              commentLine++;              normalLine++;              if (findPair(line)) {                  comment = false;              } else {                  comment = true;              }          } else if (line.startsWith("/*") && !line.endsWith("*/")) {              commentLine++;              comment = true;          } else if (line.startsWith("/*") && line.endsWith("*/")) {              commentLine++;              comment = false;          } else if (line.matches(".+/\\*.*") && !line.endsWith("*/")) {              commentLine++;              normalLine++;              if (findPair(line)) {                  comment = false;              } else {                  comment = true;              }          } else if (line.matches(".+/\\*.*") && line.endsWith("*/")) {              commentLine++;              normalLine++;              comment = false;          } else {              normalLine++;          }      }        private static boolean findPair(String line) { // 尋找一行中/*與*/是否成對出現          int count1 = 0;          int count2 = 0;          Pattern p = Pattern.compile("/\\*");          Matcher m = p.matcher(line);          while (m.find()) {              count1++;          }          p = Pattern.compile("\\*/");          m = p.matcher(line);          while (m.find()) {              count2++;          }          return (count1 == count2);      }  }

 

Java 程式碼統計(轉)

聯繫我們

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