java實現(xiàn)代碼統(tǒng)計小程序
更新時間:2019年09月23日 08:35:27 作者:_初六
這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)代碼統(tǒng)計小程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了java代碼統(tǒng)計小程序,供大家參考,具體內(nèi)容如下
可以測試每周你的工作量
package rexExp;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class CodeCounter {
//三個靜態(tài)變量存儲行數(shù)
static long normalLines = 0;
static long commentLines = 0;
static long whileLines = 0;
public static void main(String[] args) {
String pathname = "E:\\testeclipseworkspace\\JavaLearn\\src\\collection";
File file = new File(pathname);
File[] codeFiles = file.listFiles();//找到文件夾下面的所有子文件
//文件必須是以.java結(jié)尾,用正則表達(dá)式來驗證
for(File child : codeFiles){
if (child.getName().matches(".*\\.java$")) {
parse(child);
}
}
System.out.println("normalLines:" + normalLines);
System.out.println("commentLines:" + commentLines);
System.out.println("whileLines:" + whileLines);
}
private static void parse(File file) {
BufferedReader bReader = null;
boolean comment = false;
try {
bReader = new BufferedReader(new FileReader(file));
//讀其中的每一行
String line = "";
while((line=bReader.readLine()) != null){
line = line.trim();//去掉首尾空格
//統(tǒng)計空行的行數(shù)
if (line.matches("^[\\s&&[^\\n]]*$")) {
whileLines++;
}
//統(tǒng)計注釋的行數(shù)
else if (line.startsWith("/*") && !line.endsWith("*/")) {
commentLines++;
//如果遇到"/*",說明注釋開始了
comment = true;
}
else if (line.startsWith("/*") && line.endsWith("*/")) {
commentLines++;
}
else if (true == comment) {
commentLines++;
if (line.endsWith("*/")) {
comment = false;
}
}
else if(line.startsWith("http://")){
commentLines++;
}
else {
normalLines++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (bReader != null) {
try {
bReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
SpringBoot如何通過配置文件(yml,properties)限制文件上傳大小
這篇文章主要介紹了SpringBoot如何通過配置文件(yml,properties)限制文件上傳大小,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
springboot實現(xiàn)修改請求狀態(tài)404改為200
這篇文章主要介紹了springboot實現(xiàn)修改請求狀態(tài)404改為200方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
SpringBoot整合EasyCaptcha實現(xiàn)圖形驗證碼功能
這篇文章主要介紹了SpringBoot整合EasyCaptcha實現(xiàn)圖形驗證碼功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-02-02
java使用DelayQueue實現(xiàn)延時任務(wù)
項目中經(jīng)常會用到類似一些需要延遲執(zhí)行的功能,比如緩存,java提供了DelayQueue來很輕松的實現(xiàn)這種功能,下面小編就來和大家介紹一下如何使用DelayQueue實現(xiàn)延時任務(wù)吧2023-10-10
解決Eclipse Tomcat OutOfMemoryError:PermGen space的問題
今天小編就為大家分享一篇關(guān)于解決Eclipse Tomcat OutOfMemoryError:PermGen space的問題,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
Java AQS(AbstractQueuedSynchronizer)源碼解析
AbstractQueuedSynchronizer被稱為隊列同步器,簡稱為大家熟知的AQS,這個類可以稱作concurrent包的基礎(chǔ)。本文將通過剖析源碼來看看AQS是如何工作的,感興趣的可以了解一下2023-02-02

