一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - java工具類之實現java獲取文件行數

java工具類之實現java獲取文件行數

2019-11-16 14:11java教程網 JAVA教程

這篇文章主要介紹了一個java工具類,可以取得當前項目中所有java文件總行數,代碼行數,注釋行數,空白行數,需要的朋友可以參考下

工具類代碼,取得當前項目中所有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);
 }
}

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲精品福利一区二区在线观看 | 国产日日干 | 国产精品吹潮香蕉在线观看 | 深夜a| 成人女人天堂午夜视频 | 媳妇和公公小说 | 男插女的下面免费视频夜色 | 精品国产原创在线观看视频 | 无码乱人伦一区二区亚洲 | 高清一区| 色综合综合 | 亚洲美日韩 | 97视频免费人人观看人人 | 日本视频高清免费观看xxx | 日韩视频在线精品视频免费观看 | 高清不卡一区二区 | 别停好爽好深好大好舒服视频 | 91禁漫 | 男人操女人视频 | 国产在线精品亚洲第一区香蕉 | 天天狠天天透 | 四虎免费在线观看视频 | 插入肥臀 | 色戒完整版 | 久久成人伊人欧洲精品AV | 国产美女在线一区二区三区 | 爆操 | 3d动漫美女被吸乳羞羞视频 | 色哟哟精品| 成人免费一区二区三区在线观看 | 秋霞一级毛片 | 国产 日韩 欧美视频二区 | 天天摸日日舔 | 精品国产欧美一区二区五十路 | 精品国产一区二区三区久久久狼 | 369看片网| 久久免费看少妇高潮A片特爽 | 国产伦精品一区二区 | 日韩免费观看 | 亚洲不卡视频在线 | 男女发生性关系视频 |