java實(shí)現(xiàn)代碼統(tǒng)計(jì)小程序
本文實(shí)例為大家分享了java代碼統(tǒng)計(jì)小程序,供大家參考,具體內(nèi)容如下
可以測(cè)試每周你的工作量
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 {
//三個(gè)靜態(tài)變量存儲(chǔ)行數(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á)式來(lái)驗(yàn)證
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)計(jì)空行的行數(shù)
if (line.matches("^[\\s&&[^\\n]]*$")) {
whileLines++;
}
//統(tǒng)計(jì)注釋的行數(shù)
else if (line.startsWith("/*") && !line.endsWith("*/")) {
commentLines++;
//如果遇到"/*",說(shuō)明注釋開(kāi)始了
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();
}
}
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring中的AOP動(dòng)態(tài)代理源碼詳解
這篇文章主要介紹了Spring中的AOP動(dòng)態(tài)代理源碼詳解,AOP即面向切面編程也稱(chēng)面向方面編程,它是面向?qū)ο缶幊蘋(píng)OP的一種補(bǔ)充,目前已成為一種比較成熟的編程方式,本文就其源碼進(jìn)行解析,需要的朋友可以參考下2023-09-09
Spring中ContextLoaderListener監(jiān)聽(tīng)詳解
這篇文章主要介紹了Spring中ContextLoaderListener監(jiān)聽(tīng)詳解,SpringMVC啟動(dòng)時(shí)會(huì)啟動(dòng)WebApplicationContext類(lèi)型的容器,并且會(huì)調(diào)用之前分析的refresh方法,需要的朋友可以參考下2024-01-01
java Gui實(shí)現(xiàn)肯德基點(diǎn)餐收銀系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java Gui實(shí)現(xiàn)肯德基點(diǎn)餐收銀系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
Mybatis-plus配置分頁(yè)插件返回統(tǒng)一結(jié)果集
本文主要介紹了Mybatis-plus配置分頁(yè)插件返回統(tǒng)一結(jié)果集,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Spring下token過(guò)期時(shí)間分平臺(tái)(web和app)設(shè)置方法
本文詳細(xì)介紹了在Spring環(huán)境下,針對(duì)web端和APP端實(shí)現(xiàn)不同token過(guò)期時(shí)間的方法,通過(guò)整合SpringBoot、springSecurity和JWT框架,文章講解了登錄流程、JWT的基本組成以及token鑒權(quán)的核心步驟,需要的朋友可以參考下2024-10-10
SpringBoot整合token實(shí)現(xiàn)登錄認(rèn)證的示例代碼
本文主要介紹了SpringBoot整合token實(shí)現(xiàn)登錄認(rèn)證的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
SpringBoot在IDEA中實(shí)現(xiàn)熱部署的步驟
這篇文章主要介紹了SpringBoot在IDEA中實(shí)現(xiàn)熱部署的步驟,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-11-11
Java中的CopyOnWriteArrayList深入解讀
這篇文章主要介紹了Java中的CopyOnWriteArrayList深入解讀,在 ArrayList 的類(lèi)注釋上,JDK 就提醒了我們,如果要把 ArrayList 作為共享變量的話,是線程不安全的,需要的朋友可以參考下2023-12-12

