package cn.edu.csu.duliang;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class DirAnalyser {
private final static String JAVA_FILE = ".java"; // java 文件的后缀
private final static String SAVE_FILE = "d:/log.txt"; // 记录日志的文件名
private int commLine = 0; // 注释的行数
private int commLineA = 0; // 最终注释行数
private int codeLine = 0; // 代码的行数
private int codeLineA = 0; // 最终代码行数
public int puclassNum = 0; // 公共类个数
public int prclassNum = 0; // 私有类个数
public int proclassNum = 0; // 保护类个数
public int intNum = 0; // 接口个数
public int i;
public int j = 0;
// 如果指定的文件不存在会抛出空指针错
public String method(String myPath) throws NullPointerException {
DirAnalyser da=new DirAnalyser();
da.listFile(new File(myPath));
String result=da.countout();
return result;
}
public void listFile(File file) {
String strTemp = "";
// 如果输入的是一个文件就直接将文件的全路径直接打印出来
if (file.isFile()) {
strTemp = file.getAbsolutePath();
// 如果该文件是一个 java 文件就统计它的代码和注释的行数
if (file.getAbsolutePath().endsWith(DirAnalyser.JAVA_FILE)) {
System.out.println("File :" + strTemp);
j++;
codeLine = 0;
commLine = 0;
this.countLines(strTemp);
this.countClasses(strTemp);
codeLineA=codeLineA+codeLine;
commLineA=commLineA+commLine;
System.out.println("代码行数:"+codeLine);
System.out.println("注释行数:"+commLine);
}
} else {
// 如果是一个目录就递归调用 listFile 方法将当前目录以及其子目录下的文件都列出来
//System.out.println("Dir :" + file.getAbsolutePath());
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
listFile(files[i]);
}
}
// 将分析的结果写入日志文件
this.saveToLog(DirAnalyser.SAVE_FILE);
}
public String countout(){
System.out.println("-----------------------------");
System.out.println("最终代码行数:"+codeLineA);
System.out.println("最终注释行数:"+commLineA);
System.out.println("公共类个数:"+puclassNum);
System.out.println("私有类个数:"+prclassNum);
System.out.println("保护类个数:"+proclassNum);
String result = "最终代码行数:"+codeLineA+"\r\n" +
"\r\n"+"最终注释行数:"+commLineA+"\r\n" +
"\r\n"+"公共类个数:"+puclassNum+"\r\n" +
""
+"私有类个数:"+prclassNum+"\r\n" +
""+"保护类个数:"+proclassNum+"\r\n" +
"\r\n";
i=puclassNum+prclassNum+proclassNum;
result=result+"类总数:"+i+"\r\n" +
"\r\n"+"接口数:"+intNum+"\r\n" +
"\r\n"+"java文件数:"+j;
System.out.println("类总数:"+i);
System.out.println("接口数:"+intNum);
System.out.println("java文件数:"+j);
return result;
}
// 统计 java 文件代码和注释行数的方法
private void countLines(String strFile) {
try {
// 设置读取的文件
BufferedReader br = new BufferedReader(new FileReader(strFile));
// 用于放置字符串的临时变量
String strTemp = null;
String strClass ="public class";
// 标识是否在 /* */ 这种注释中
boolean isComm = false;
while ((strTemp = br.readLine()) != null) {
// 空行不统计
if (strTemp.trim().equals(""))
continue;
if (strTemp.trim().startsWith("//")) {
if(isComm==false)
this.commLine++;
continue;
}
if (strTemp.trim().startsWith("/*")) {
isComm = true;
}
if (strTemp.trim().endsWith("*/")) {
isComm = false;
this.commLine++;
continue;
}
if (isComm) {
this.commLine++;
} else {
this.codeLine++;
}
}
// 关闭输入
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 统计 java 文件代码和注释行数的方法
private void countClasses(String strFile) {
try {
// 设置读取的文件
BufferedReader br = new BufferedReader(new FileReader(strFile));
// 用于放置字符串的临时变量
String strTemp = null;
String strpuClass ="public class";
String strpuFClass ="public final class";
String strproFClass ="final class";
String strOClass ="class";
String strAClass ="abstract class";
String strPClass ="public abstract class";
String strprClass ="private class";
String strproClass ="protected class";
String strInt = "interface";
String strIntO = "public interface";
while ((strTemp = br.readLine()) != null) {
if (strTemp.trim().startsWith(strpuClass) || strTemp.trim().startsWith(strPClass) ||
strTemp.trim().startsWith(strpuFClass)) {
this.puclassNum++;
continue;
}
if (strTemp.trim().startsWith(strprClass)) {
this.prclassNum++;
continue;
}
if (strTemp.trim().startsWith(strproClass) || strTemp.trim().startsWith(strOClass) || strTemp.trim().startsWith(strAClass)
|| strTemp.trim().startsWith(strproFClass)) {
this.proclassNum++;
continue;
}
if (strTemp.trim().startsWith(strInt) || strTemp.trim().startsWith(strIntO)) {
this.intNum++;
continue;
}
}
// 关闭输入
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 保存记录文件的方法
private void saveToLog(String strFile) {
try {
// 设置记录文件
BufferedWriter bw = new BufferedWriter(new FileWriter(strFile));
String strTemp = "";
// 往文件写入第一行内容
strTemp = "commentary : " + this.commLine;
bw.write(strTemp);
// 往文件写入第二行的内容
bw.newLine();
strTemp = "code : " + this.codeLine;
bw.write(strTemp);
// 清空流里面的内容
bw.flush();
// 关闭输出流
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
统计项目代码量(附源码)
需积分: 25 171 浏览量
2013-10-08
20:29:43
上传
评论 1
收藏 14KB ZIP 举报
站在巨人的肩膀上奋斗
- 粉丝: 75
- 资源: 44
最新资源
- AIS2024 valid
- 最入门的爬虫代码 python.docx
- 爬虫零基础入门-爬取天气预报.pdf
- 最通俗易懂的 MongoDB 非结构化文档存储数据库教程.zip
- 以mongodb为数据库的订单物流小项目.zip
- 腾讯云-mongodb数据库, 项目部署.zip
- 腾讯 APIJSON 的 MongoDB 数据库插件.zip
- 理解非关系型数据库和关系型数据库的区别.zip
- 操作简单的Mongodb网页web管理工具,基于Spring Boot2.0支持mongodb集群.zip
- tms-mongodb-web,提供访问mongodb数据的REST API和可灵活扩展的mongodb web 客户端.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈