欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java工具類之實(shí)現(xiàn)java獲取文件行數(shù)

 更新時(shí)間:2014年03月28日 09:06:34   作者:  
這篇文章主要介紹了一個(gè)java工具類,可以取得當(dāng)前項(xiàng)目中所有java文件總行數(shù),代碼行數(shù),注釋行數(shù),空白行數(shù),需要的朋友可以參考下

工具類代碼,取得當(dāng)前項(xiàng)目中所有java文件總行數(shù),代碼行數(shù),注釋行數(shù),空白行數(shù)

復(fù)制代碼 代碼如下:

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;  //總行數(shù)    
 private static int source  = 0;  //代碼行數(shù)   
 private static int blank  = 0;  //空白行數(shù)    
 private static int comments = 0; //注釋行數(shù)

 /**
  * 讀取文件夾內(nèi)java文件
  * @param dir
  */
 private static void listNext(File dir) {
  File[] files = dir.listFiles();
  for (int i = 0; i < files.length; i++) {
   //判斷是否是文件夾,如果是文件夾繼續(xù)向下檢索
   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文件總行數(shù),代碼行數(shù),空白行數(shù),注釋行數(shù)
  * @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文件以字符數(shù)組形式讀取
  * @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("");//參數(shù)為空
  //獲取項(xiàng)目路徑
  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);
 }
}

相關(guān)文章

  • Java中的Optional類詳細(xì)解讀

    Java中的Optional類詳細(xì)解讀

    這篇文章主要介紹了Java中的Optional類詳細(xì)解讀,Optional是Java中的一個(gè)類,它的作用是用于解決空指針異常的問題,它提供了一些有用的方法,可以幫助我們避免顯式進(jìn)行空值檢測,需要的朋友可以參考下
    2023-08-08
  • 詳解Java中多進(jìn)程編程的實(shí)現(xiàn)

    詳解Java中多進(jìn)程編程的實(shí)現(xiàn)

    這篇文章主要介紹了詳解Java中多進(jìn)程編程的實(shí)現(xiàn),和多線程一樣,多進(jìn)程同樣是實(shí)現(xiàn)并發(fā)的一種方式,需要的朋友可以參考下
    2015-11-11
  • 使用jenkins部署springboot項(xiàng)目的方法步驟

    使用jenkins部署springboot項(xiàng)目的方法步驟

    這篇文章主要介紹了使用jenkins部署springboot項(xiàng)目的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 一文詳解Mybatis-plus的介紹與使用

    一文詳解Mybatis-plus的介紹與使用

    Mybatis-Plus?是?MyBatis?的一個(gè)增強(qiáng)工具,專門針對(duì)于傳統(tǒng)MyBatis開發(fā)中sql需要手動(dòng)進(jìn)行映射配置繁瑣缺點(diǎn)的一款框架技術(shù)。本文將為大家詳細(xì)講講Mybatis-plus的介紹與使用,感興趣的可以了解一下
    2022-07-07
  • Java中的線程生命周期核心概念

    Java中的線程生命周期核心概念

    這篇文章主要介紹了Java中的線程生命周期核心概念,通過使用一個(gè)快速的圖解展開文章內(nèi)容,需要的小伙伴可以參考一下
    2022-06-06
  • SpringMVC響應(yīng)處理詳細(xì)解讀

    SpringMVC響應(yīng)處理詳細(xì)解讀

    Spring?MVC?是?Spring?提供的一個(gè)基于?MVC?設(shè)計(jì)模式的輕量級(jí)?Web?開發(fā)框架,本質(zhì)上相當(dāng)于?Servlet,Spring?MVC?角色劃分清晰,分工明細(xì),本章來講解SpringMVC數(shù)據(jù)響應(yīng)
    2022-07-07
  • 利用spring boot如何快速啟動(dòng)一個(gè)web項(xiàng)目詳解

    利用spring boot如何快速啟動(dòng)一個(gè)web項(xiàng)目詳解

    這篇文章主要給大家介紹了關(guān)于利用spring boot如何快速啟動(dòng)一個(gè)web項(xiàng)目的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧、
    2017-12-12
  • Java多線程 原子性操作類的使用

    Java多線程 原子性操作類的使用

    這篇文章主要介紹了Java多線程 原子性操作類的使用,在java5以后,我們接觸到了線程原子性操作,也就是在修改時(shí)我們只需要保證它的那個(gè)瞬間是安全的即可,經(jīng)過相應(yīng)的包裝后可以再處理對(duì)象的并發(fā)修改,本文總結(jié)一下Atomic系列的類的使用方法,下面一起進(jìn)入文章了解詳細(xì)內(nèi)容
    2021-10-10
  • Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(37)

    Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(37)

    下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-07-07
  • 淺談Spring的屬性編輯器的使用

    淺談Spring的屬性編輯器的使用

    這篇文章主要介紹了淺談Spring的屬性編輯器的使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-05-05

最新評(píng)論