代码量统计可以统计去掉注释和空行的代码量.docx
《代码量统计可以统计去掉注释和空行的代码量.docx》由会员分享,可在线阅读,更多相关《代码量统计可以统计去掉注释和空行的代码量.docx(8页珍藏版)》请在冰点文库上搜索。
代码量统计可以统计去掉注释和空行的代码量
代码量统计,可以统计去掉注释和空行的代码量
本文是关于代码量统计的,你可以统计去掉注释和空行的代码量,可以显示代码量的行数,注视的行数和空行数
代码如下:
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileReader;
importjava.io.IOException;
importjava.util.ArrayList;
publicclassCodeCounter{
staticlong
codeLines=0;
staticlong
commentLines=0;
staticlong
blankLines=0;
static
ArrayList<File>fileArray=new
ArrayList<File>();
public
staticvoidmain(String[]args){
//可以统计指定目录下以及其子目录下的所有java文件中代码
Filefile=new
File("D:
//worspace//工程文件路径");
ArrayList<File>al=getFile(file);
for(Filef:
al){
//
if(f.getName().matches(".*\\.java$"))//匹配java格式的文件
//
if
(f.getName().matches(".*\\.xml$"))//匹配xml格式的文件
//
if(f.getName().matches(".*\\.sql$"))//匹配sql格式的文件
if(f.getName().matches(".*\\.properties$"))//
匹配properties格式的文件
//
if(f.getName().matches(".*\\.jsp$"))//
匹配jsp格式的文件
//
if(f.getName().matches(".*\\.js$"))//匹配js格式的文件
count(f);
}
System.out.println("代码行数:
"+codeLines);
System.out.println("注释行数:
"+commentLines);
System.out.println("空白行数:
"+blankLines);
}
//
获得目录下的文件和子目录下的文件
public
staticArrayList<File>getFile(File
f){
File[]ff=f.listFiles();
for(Filechild:
ff){
if(child.isDirectory()){
getFile(child);
}else
fileArray.add(child);
}
returnfileArray;
}
//统计方法
private
staticvoidcount(Filef){
BufferedReaderbr=null;
booleanflag=false;
try{
br=newBufferedReader(newFileReader(f));
Stringline="";
while((line=br.readLine())!
=null){
line=line.trim();//除去注释前的空格
if(line.matches("^[]*$")){//匹配空行
blankLines++;
}elseif(line.startsWith("//")){
commentLines++;
}elseif(line.startsWith("")){
commentLines++;
flag=true;
}elseif(line.startsWith("")){
commentLines++;
}elseif(flag==true){
commentLines++;
if(line.endsWith("*/")){
flag=false;
}
}else{
codeLines++;
}
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(br!
=null){
try{
br.close();
br=null;
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
}