java工具類之實(shí)現(xiàn)java獲取文件行數(shù)
工具類代碼,取得當(dāng)前項(xiàng)目中所有java文件總行數(shù),代碼行數(shù),注釋行數(shù),空白行數(shù)
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中多進(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)目的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04利用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-12Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(37)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07