
正文
java统计有效代码行 java代码行数统计工具
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Java 有什么好的代码行数,注释行数统计工具
package com.syl.demo.test;
import java.io.*;
/**
* java代码行数统计工具类
* Created by 孙义朗 on 2017/11/17 0017.
*/
public class CountCodeLineUtil {
private static int normalLines = 0; //有效程序行数
private static int whiteLines = 0; //空白行数
private static int commentLines = 0; //注释行数
public static void countCodeLine(File file) {
System.out.println("代码行数统计java统计有效代码行:" + file.getAbsolutePath());
if (file.exists()) {
try {
scanFile(file);
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("文件不存在java统计有效代码行!");
System.exit(0);
}
System.out.println(file.getAbsolutePath() + " ,java文件统计java统计有效代码行:" +
"总有效代码行数: " + normalLines +
" ,总空白行数:" + whiteLines +
" ,总注释行数:" + commentLines +
" ,总行数:" + (normalLines + whiteLines + commentLines));
}
private static void scanFile(File file) throws IOException {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i files.length; i++) {
scanFile(files[i]);
}
}
if (file.isFile()) {
if (file.getName().endsWith(".java")) {
count(file);
}
}
}
private static void count(File file) {
BufferedReader br = null;
// 判断此行是否为注释行
boolean comment = false;
int temp_whiteLines = 0;
int temp_commentLines = 0;
int temp_normalLines = 0;
try {
br = new BufferedReader(new FileReader(file));
String line = "";
while ((line = br.readLine()) != null) {
line = line.trim();
if (line.matches("^[//s[^//n]]*$")) {
// 空行
whiteLines++;
temp_whiteLines++;
} else if (line.startsWith("/*") !line.endsWith("*/")) {
// 判断此行为"/*"开头java统计有效代码行的注释行
commentLines++;
comment = true;
} else if (comment == true !line.endsWith("*/")) {
// 为多行注释中的一行(不是开头和结尾)
commentLines++;
temp_commentLines++;
} else if (comment == true line.endsWith("*/")) {
// 为多行注释的结束行
commentLines++;
temp_commentLines++;
comment = false;
} else if (line.startsWith("//")) {
// 单行注释行
commentLines++;
temp_commentLines++;
} else {
// 正常代码行
normalLines++;
temp_normalLines++;
}
}
System.out.println(file.getName() +
" ,有效行数" + temp_normalLines +
" ,空白行数" + temp_whiteLines +
" ,注释行数" + temp_commentLines +
" ,总行数" + (temp_normalLines + temp_whiteLines + temp_commentLines));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//测试
public static void main(String[] args) {
File file = new File("F:\\myweb");
countCodeLine(file);
}
}
相关问答
Q1: 如何统计某目录下的java文件代码行数
可以自己写一个小程序,遍历每个文件;
如果是*.java就记录该文件的行数,依次累加。
Q2: eclipse怎么统计代码行数
步骤如下:
1、打开File Search对话框。
2、选中正则表达式,在搜索文本框输入\n 。
3、文件名称输入 *.java。
4、在范围里选中Enclosing projects。
经过上面方式,就可以统计出整个项目的代码行数。
Q3: 求问怎么统计JAVA代码行数?有什么工具?
源代码行数统计器 1.5
本软件用于统计软件工程源代码行数,
可对指定的子目录下或整个目录树中所有指定类型的源代码文件进行行数统计。
Q4: java web项目怎么计算有效代码行数?希望有大虾能告诉我
在MyEcplise或者Ecplise中。在编辑窗口的最左侧右键。Show
line
row
number。就可以显示你编辑的代码有多少行了。希望对你有帮助!
Q5: 急!急!急!急!急! 在一个文件夹目录下 如果是.java文件就分析出有效代码行数、空行数、注释行数。接下面
我们以前在学校的时候也做过这个,还要用Swing做界面。不过今天是我从新做出来的
JavaSourceUtil.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
class JavaSourceUtil {
public static void main(String[] args) {
// 统计文件路径
String folderPath = "C:\\Users\\xmi\\Desktop\\AnzDoc\\123\\BatchAssistant\\src\\dcb\\";
// 路径文件
File folder = new File(folderPath);
// 源文件统计工具
JavaSourceUtil util = new JavaSourceUtil();
// 根据路径得到统计结果列表
ListSourceBean cntList = util.countJavaSource(folder);
// 根据统计行数计算注释率、空行率、有效代码率的结果
StringBuffer resultSbuf = util.outputCountResult(cntList);
// 输出统计结果
System.out.println(resultSbuf.toString());
// 保存结果到文件
util.saveFile(resultSbuf, "D:\\java_source_cnt.txt");
}
public StringBuffer outputCountResult(ListSourceBean listBean){
StringBuffer sbuf = new StringBuffer();
SourceBean totalCntBean = new SourceBean("全部文件统计");
for(SourceBean bean : listBean) {
int tolCnt = bean.getTotalLine();
if (tolCnt == 0) {
continue;
}
// 注释率、空行率、有效代码率的结果算出来
sbuf.append(bean.fileName + "代码统计:").append("\r\n");
sbuf.append("空行率:").append(bean.blankLine * 1.0 / tolCnt)
.append("\r\n");
sbuf.append("注释率:")
.append((bean.singlgCmtLine + bean.multCmtLine) * 1.0
/ tolCnt).append("\r\n");
sbuf.append("有效代码率:").append(bean.codeLine * 1.0 / tolCnt)
.append("\r\n\r\n");
totalCntBean.blankLine += bean.blankLine;
totalCntBean.codeLine += bean.codeLine;
totalCntBean.documtLine += bean.documtLine;
totalCntBean.multCmtLine += bean.multCmtLine;
totalCntBean.singlgCmtLine += bean.singlgCmtLine;
}
sbuf.append(totalCntBean.fileName + "代码统计:").append("\r\n");
sbuf.append("总空行率:").append(totalCntBean.blankLine * 1.0 / totalCntBean.getTotalLine())
.append("\r\n");
sbuf.append("总注释率:")
.append((totalCntBean.singlgCmtLine + totalCntBean.multCmtLine) * 1.0
/ totalCntBean.getTotalLine()).append("\r\n");
sbuf.append("总有效代码率:").append(totalCntBean.codeLine * 1.0 / totalCntBean.getTotalLine())
.append("\r\n\r\n");
return sbuf;
}
/**
* Java代码统计
* @param folder 基本路径
* @return 统计结果
*/
public ListSourceBean countJavaSource(File folder){
ListSourceBean cntList = new ArrayListSourceBean();
if (!folder.exists() || !folder.isDirectory()) {
return cntList;
}
File[] files = folder.listFiles();
for (int i = 0; i files.length; i++) {
if (files[i].isDirectory()) {
cntList.addAll(countJavaSource(folder));
} else {
SourceBean cntBean = countCodeLine(files[i]);
if (cntBean != null) {
cntList.add(cntBean);
}
}
}
return cntList;
}
/**
* 单个文件代码统计
* @param javaFile .java文件
* @return 统计结果
*/
public SourceBean countCodeLine(File javaFile){
if(!javaFile.exists() || !javaFile.isFile() || !javaFile.getName().endsWith(".java")){
return null;
}
SourceBean cntBean = new SourceBean(javaFile.getAbsolutePath());
// 代码状态
int status = 0;
try {
InputStreamReader isr = new InputStreamReader(new FileInputStream(javaFile));
String str = "";
BufferedReader bufReader = new BufferedReader(isr);
while((str = bufReader.readLine()) != null){
str = str.trim();
if (str.startsWith("/**")) {
// 文档注释
status = 10;
} else if (str.startsWith("/*")) {
// 多行注释
status = 20;
}
if (isEmpty(str)) {
// 空白航
cntBean.blankLine ++;
} else if (status == 10) {
// 文档注释
cntBean.documtLine ++;
} else if (status == 20) {
// 多行注释
cntBean.multCmtLine ++;
} else if (str.startsWith("//")) {
// 单行注释
cntBean.singlgCmtLine ++;
} else {
// 代码行
cntBean.codeLine ++;
}
if (str.endsWith("*/")) {
// 单行或文本注释结束
status = 0;
}
}
bufReader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return cntBean;
}
/**
* 空行判断
* @param strValue
* @return 是否为空行
*/
public boolean isEmpty(String strValue){
if (strValue == null || strValue.trim().equals("")) {
return true;
}
return false;
}
/**
* 读取文本文件
* @param path
* @return
*/
public void saveFile(StringBuffer builder, String path){
try {
FileOutputStream fos = new FileOutputStream(path);
fos.write(builder.toString().getBytes("utf-8"));
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
SourceBean.java
public class SourceBean {
// 空白行
public int blankLine;
// 单行注释
public int singlgCmtLine;
// 多行注释
public int multCmtLine;
// 代码行数
public int codeLine;
// 文挡注释
public int documtLine;
// 文件名
public String fileName;
public SourceBean(String fileName){
this.fileName = fileName;
}
public int getTotalLine(){
int totalLine = blankLine + singlgCmtLine + multCmtLine + codeLine
+ documtLine;
return totalLine;
}
}
是不是很简单!
关于java统计有效代码行和java代码行数统计工具的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






