java工具類之實現java擷取檔案行數_java

來源:互聯網
上載者:User

工具類代碼,取得當前項目中所有java檔案總行數,程式碼數,注釋行數,空白行數

複製代碼 代碼如下:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Administrator
 *
 */
public class JavaCode {
 private static final String PROJECT_DIR = "C:\\test";
 private static int totle = 0;  //總行數    
 private static int source  = 0;  //程式碼數   
 private static int blank  = 0;  //空白行數    
 private static int comments = 0; //注釋行數

 /**
  * 讀取檔案夾內java檔案
  * @param dir
  */
 private static void listNext(File dir) {
  File[] files = dir.listFiles();
  for (int i = 0; i < files.length; i++) {
   //判斷是否是檔案夾,如果是檔案夾繼續向下檢索
   if (files[i].isDirectory()) {
    listNext(files[i]);
   } else {
    try {
     if (files[i].getName().endsWith(".java")) {
      System.out.println(files[i].getAbsolutePath());
      javaLine(files[i]);
     }
    }catch (Exception e) {
     e.printStackTrace();
    }
   }
  }
 }

 /**
  * 讀取java檔案總行數,程式碼數,空白行數,注釋行數
  * @param f
  * @throws IOException
  * @throws FileNotFoundException
  */
 private static void javaLine(File f) throws FileNotFoundException, IOException{
  String strLine = "";
  String str = fromFile(f);
  if (str.length() > 0) {
   while (str.indexOf('\n') != -1) {
    totle++;
    //System.out.println(totle);
    strLine = str.substring(0, str.indexOf('\n')).trim();
    //str = str.substring(str.indexOf('\n') + 1, str.length());
    if (strLine.length() == 0 ) {
     blank++;
    }else if (strLine.charAt(0) == '*' || strLine.charAt(0) == '/') {
     comments++;
    }else{
     source++;
     String regEx = "^*//";
     if(regEx(strLine,regEx)){
      comments++;
     }
    }
    str = str.substring(str.indexOf('\n') + 1, str.length());
   }
  } 
 }

 /**
  * 將java檔案以字元數組形式讀取
  * @param f
  * @return
  * @throws FileNotFoundException
  * @throws IOException
  */
 private static String  fromFile(File f) throws FileNotFoundException,IOException {
  FileInputStream fis = new FileInputStream(f);
  byte[] b = new byte[(int) f.length()];
  fis.read(b);
  fis.close();
  return new String(b);
 }

 /**
  * 正則匹配
  * @param str 輸入字串
  * @param regEx 正則匹配字串
  * @return
  */
 private static boolean regEx(String str,String regEx){
  Pattern p=Pattern.compile(regEx);
  Matcher m=p.matcher(str);
  boolean result=m.find();
  return result;
 }
 public static void main(String[] args) throws FileNotFoundException, IOException {
  //File root = new File(PROJECT_DIR);
  File directory = new File("");//參數為空白
  //擷取項目路徑
  String projectFile = directory.getCanonicalPath() ;
  System.out.println(projectFile+"===============");
  listNext(new File(projectFile));
  System.out.println(totle+1);
  System.out.println(source);
  System.out.println(blank);
  System.out.println(comments);
 }
}

聯繫我們

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