java工具類(lèi)之實(shí)現(xiàn)java獲取文件行數(shù)
工具類(lèi)代碼,取得當(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)文章
springboot zuul實(shí)現(xiàn)網(wǎng)關(guān)的代碼
這篇文章主要介紹了springboot zuul實(shí)現(xiàn)網(wǎng)關(guān)的代碼,在為服務(wù)架構(gòu)體系里,網(wǎng)關(guān)是非常重要的環(huán)節(jié),他實(shí)現(xiàn)了很多功能,具體哪些功能大家跟隨小編一起通過(guò)本文學(xué)習(xí)吧2018-10-10
Mybatis+Druid+MybatisPlus多數(shù)據(jù)源配置方法
在項(xiàng)目開(kāi)發(fā)中,經(jīng)常需要連接多個(gè)數(shù)據(jù)庫(kù),使用Mybatis、Druid和MybatisPlus可以實(shí)現(xiàn)多數(shù)據(jù)源配置,通過(guò)定義配置類(lèi)和修改配置文件,如properties或yaml,可以設(shè)置多個(gè)數(shù)據(jù)源,本文介紹了配置項(xiàng)包括Druid基本配置、數(shù)據(jù)源一、數(shù)據(jù)源二,感興趣的朋友一起看看吧2024-09-09
java設(shè)計(jì)模式原型模式與享元模式調(diào)優(yōu)系統(tǒng)性能詳解
這篇文章主要為大家介紹了java設(shè)計(jì)模式原型模式與享元模式調(diào)優(yōu)系統(tǒng)性能方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Spring Boot Rest控制器單元測(cè)試過(guò)程解析
這篇文章主要介紹了Spring Boot Rest控制器單元測(cè)試過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Java如何將String轉(zhuǎn)換成json對(duì)象或json數(shù)組
這篇文章主要介紹了Java如何將String轉(zhuǎn)換成json對(duì)象或json數(shù)組,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02

