1.故事发生原因
13号从公司辞职后,16号入职新公司去联通做外包测试。大概一星期左右,头头儿说项目发奖金,您说我这是点儿正还是点儿背,嘿嘿~
言归正传,发奖金也要有个标准,根据代码量,代码质量,编写规范等等方面做标准,然后我的leader,要求我跟另外一个同事搞定代码量的工作。
我擦~我从网上下载了几个代码行数统计工具,可以说做的很专业,比如sourceCounter.可以统计代码总行数,有效代码,空行,注释代码等等,但是不涵盖开发人员,另外就是要导出excel格式的话,你要花钱注册的,反正挺费事的~为了发扬“免费的”传统,自己研究写了几行代码,基本上可以实现以上要求
2.功能基本介绍
输入工程所在路径,开发人员名单,导出文件路径
一个工程大致有1000多个java文件,我就是统计这些文件的文件名 路径 总行数 代码行数 注释行数 空行数 开发人员 并导出csv文件
3.源文件代码 各位大神可以帮忙优化,毕竟我只是个测试的,开发能力有限
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class ReadFile {
public String makePath(String fileSavePath ){
File fileSave = new File(fileSavePath);
FileWriter fw = null;
if(!fileSave.exists()){
try {
fileSave.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("文件创建失败!");
}
}
try {
fw = new FileWriter(fileSavePath) ;
fw.write("文件名,路径,总行数,代码行数,注释行数,空行数,开发人员"+"\n");
fw.flush();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("文件写入失败!");
}
return fileSavePath;
}
/**
*
* @param filePath 工程路径
* @param fileSavePath 保存路径
* @param developerName 开发人员名单
* @param fileType 统计文件类型 .java .net
*/
public void rdFile(String filePath,String fileSavePath,String []developerName,String fileType) {
File file = new File(filePath);
FileWriter fw = null;
if(!file.isDirectory()){
System.out.println("文件名:"+file.getName());
}else if(file.isDirectory()){
String[] list = file.list();
for(int i =0;i<list.length;i++){
File files = new File(filePath+"\\"+list[i]);
if(!files.isDirectory()&&files.getName().endsWith(fileType)){
// System.out.println("文件名:"+files.getPath());
try {
fw = new FileWriter(fileSavePath,true) ;
//文件名,路径,总行数,代码行数,注释行数,空行数,开发人员
fw.write(files.getName()+","+
files.getPath()+","+
readFileByLine(files.getPath(),developerName).get("0")+","+
readFileByLine(files.getPath(),developerName).get("1")+","+
readFileByLine(files.getPath(),developerName).get("2")+","+
readFileByLine(files.getPath(),developerName).get("3")+","+
readFileByLine(files.getPath(),developerName).get("4")+","+
"\n"
);
fw.flush();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("文件写入异常");
}
}else if(files.isDirectory()){
//递归
rdFile(filePath+"\\"+list[i],fileSavePath,developerName,fileType);
}
}
}
}
@SuppressWarnings("unchecked")
/**
*
* @param filePath
* @param developerName
* @return
*/
public Map readFileByLine(String filePath,String []developerName) {
Map mapInfo = new HashMap();
File file = new File(filePath);
BufferedReader bd = null;
//总行数
int lineT = 0;
//代码行数
int lineCode = 0;
//注释行数
int lineMark = 0 ;
//空行数
int lineBlank = 0;
//开发人员名字
String Name = "无名";
//
try {
System.out.println("按行读取文件,一次读取一行:");
System.out.println("\n==========start reading==========");
bd = new BufferedReader(new FileReader(file));
String temp = "";
while ((temp = bd.readLine()) != null) {
if(temp.length() > 0) {
System.out.println(temp);
//当以【/*,*/,//,*】开头的行,即为注释行,*/结尾
if(temp.trim().startsWith("/*")||temp.trim().startsWith("*/")||temp.trim().startsWith("*")||temp.trim().startsWith("//")||temp.trim().endsWith("*/")){
if(Name.equals("无名")){
for(String name:developerName){
if(temp.contains(name)){
Name = name;
break;
}
}
}
lineMark++;
}else{
//代码行数增加
lineCode++;
}
}else {
//空行增加
lineBlank++;
}
lineT++;
}
System.out.println("\n==========end==========");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(bd != null) bd.close();
} catch (IOException e) {
e.printStackTrace();
}
}
mapInfo.put("0", lineT);
mapInfo.put("1", lineCode);
mapInfo.put("2", lineMark);
mapInfo.put("3", lineBlank);
mapInfo.put("4", Name);
return mapInfo;
}
public static void main(String[] args){
//开发人员名单
String [] developerName ={"wangfuhai","zhangchunji","changjiwen","lianning","zhangsan"};
//所要统计的文件路径
String sourcePath = "G:\\联通\\cif";
//所要统计的文件类型
String fileType = ".java";
//生成的文件的路径
String fileSavePath = "C:\\counter_DENGSH\\counter1.csv";
ReadFile readFile = new ReadFile();
readFile.rdFile(sourcePath,readFile.makePath(fileSavePath),developerName,fileType);
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class ReadFile {
public String makePath(String fileSavePath ){
File fileSave = new File(fileSavePath);
FileWriter fw = null;
if(!fileSave.exists()){
try {
fileSave.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("文件创建失败!");
}
}
try {
fw = new FileWriter(fileSavePath) ;
fw.write("文件名,路径,总行数,代码行数,注释行数,空行数,开发人员"+"\n");
fw.flush();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("文件写入失败!");
}
return fileSavePath;
}
/**
*
* @param filePath 工程路径
* @param fileSavePath 保存路径
* @param developerName 开发人员名单
* @param fileType 统计文件类型 .java .net
*/
public void rdFile(String filePath,String fileSavePath,String []developerName,String fileType) {
File file = new File(filePath);
FileWriter fw = null;
if(!file.isDirectory()){
System.out.println("文件名:"+file.getName());
}else if(file.isDirectory()){
String[] list = file.list();
for(int i =0;i<list.length;i++){
File files = new File(filePath+"\\"+list[i]);
if(!files.isDirectory()&&files.getName().endsWith(fileType)){
// System.out.println("文件名:"+files.getPath());
try {
fw = new FileWriter(fileSavePath,true) ;
//文件名,路径,总行数,代码行数,注释行数,空行数,开发人员
fw.write(files.getName()+","+
files.getPath()+","+
readFileByLine(files.getPath(),developerName).get("0")+","+
readFileByLine(files.getPath(),developerName).get("1")+","+
readFileByLine(files.getPath(),developerName).get("2")+","+
readFileByLine(files.getPath(),developerName).get("3")+","+
readFileByLine(files.getPath(),developerName).get("4")+","+
"\n"
);
fw.flush();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("文件写入异常");
}
}else if(files.isDirectory()){
//递归
rdFile(filePath+"\\"+list[i],fileSavePath,developerName,fileType);
}
}
}
}
@SuppressWarnings("unchecked")
/**
*
* @param filePath
* @param developerName
* @return
*/
public Map readFileByLine(String filePath,String []developerName) {
Map mapInfo = new HashMap();
File file = new File(filePath);
BufferedReader bd = null;
//总行数
int lineT = 0;
//代码行数
int lineCode = 0;
//注释行数
int lineMark = 0 ;
//空行数
int lineBlank = 0;
//开发人员名字
String Name = "无名";
//
try {
System.out.println("按行读取文件,一次读取一行:");
System.out.println("\n==========start reading==========");
bd = new BufferedReader(new FileReader(file));
String temp = "";
while ((temp = bd.readLine()) != null) {
if(temp.length() > 0) {
System.out.println(temp);
//当以【/*,*/,//,*】开头的行,即为注释行,*/结尾
if(temp.trim().startsWith("/*")||temp.trim().startsWith("*/")||temp.trim().startsWith("*")||temp.trim().startsWith("//")||temp.trim().endsWith("*/")){
if(Name.equals("无名")){
for(String name:developerName){
if(temp.contains(name)){
Name = name;
break;
}
}
}
lineMark++;
}else{
//代码行数增加
lineCode++;
}
}else {
//空行增加
lineBlank++;
}
lineT++;
}
System.out.println("\n==========end==========");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(bd != null) bd.close();
} catch (IOException e) {
e.printStackTrace();
}
}
mapInfo.put("0", lineT);
mapInfo.put("1", lineCode);
mapInfo.put("2", lineMark);
mapInfo.put("3", lineBlank);
mapInfo.put("4", Name);
return mapInfo;
}
public static void main(String[] args){
//开发人员名单
String [] developerName ={"wangfuhai","zhangchunji","changjiwen","lianning","zhangsan"};
//所要统计的文件路径
String sourcePath = "G:\\联通\\cif";
//所要统计的文件类型
String fileType = ".java";
//生成的文件的路径
String fileSavePath = "C:\\counter_DENGSH\\counter1.csv";
ReadFile readFile = new ReadFile();
readFile.rdFile(sourcePath,readFile.makePath(fileSavePath),developerName,fileType);
}
}
4.写个批处理吧,run.bat,点击直接可调用运行java,(记得要有jdk,不然白瞎~);不愿意用bat这种方式的可随意用ide
run.bat 路径自己修改哦~
@echo off
set CLASSPATH=\%JAVA_HOME%\bin\;./bin;
javac C:\counter_DENGSH\src\ReadFile.java
java -classpath C:\counter_DENGSH\src ReadFile
@pause
5,这是我的目录结构,(仅供参考)
C:--------counter_DENGSH------run.bat
------src----------ReadFile.java
【转载注明出处】~我看别人都写这个~挂上吧!