资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

统计java代码注释率的简单介绍

急!急!急!急!急! 在一个文件夹目录下 如果是.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 有什么好的代码行数,注释行数统计工具

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("代码行数统计:" + file.getAbsolutePath());

if (file.exists()) {

try {

scanFile(file);

} catch (IOException e) {

e.printStackTrace();

}

} else {

System.out.println("文件不存在!");

System.exit(0);

}

System.out.println(file.getAbsolutePath() + " ,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("*/")) {

// 判断此行为"/*"开头的注释行

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);

}

}

对于java语言来说,大量注释会影响执行效率么

Java代码注释写的多,会影响到编译效率,但是不会影响到执行效率。

Java代码是先编译成字节码,然后被JVM解释执行的。

我做了个实验

TimeDemo 类

import java.util.ArrayList;

public class TimeDemo {

public static void main(String[] args) {

long start = System.currentTimeMillis();

ArrayListInteger list = new ArrayListInteger();

for (int i = 0; i 1000000; i++) {

list.add(i);

}

long end = System.currentTimeMillis();

System.out.println("本次执行耗费了"+(end-start)+"毫秒");

}

}

TimeDemo2

import java.util.ArrayList;

public class TimeDemo2 {

public static void main(String[] args) {

long start = System.currentTimeMillis();

ArrayListInteger list = new ArrayListInteger();

for (int i = 0; i 1000000; i++) {

list.add(i);

}

//用java.io生成了很多行的注释,

//注释

//注释

//注释

//注释

//注释

long end = System.currentTimeMillis();

System.out.println("本次执行耗费了"+(end-start)+"毫秒");

}

}

运行结果

当注释行数是1~1万行的时候. 能较快的编译

当注释行数达到1百万的时候,编译稍微慢一点

当注释行数达到1千万行的时候, CPU占用100%,卡了进1分钟也没有编译完成,只好强行关闭

结论:

简单明了的注释有助于程序猿对代码的读写

只有当注释行数极大的时候,才会严重的影响编译速度。 但不会影响执行速度

统计C++源程序中代码行数,注释行数,注释率。

文件读入

如果前两个字符不为“//”或空格则为实际代码

如果前两个字符为“//”则为注释

注释行/(注释+代码)就是注释率

统计某个文件夹下有多少个JAVA文件,并得出总共有多少代码

public static void main(String[] args) {

try {

File inFile = new File("F:/Temp");

File outFile = new File("F:/Temp/result.txt");

int sumFile = 0;//总文件数

int sumLine = 0;//总行数

FileWriter fw = new FileWriter(outFile);

BufferedWriter bw = new BufferedWriter(fw);

bw.write("类名\t\t行数");

bw.newLine();

for (File file : inFile.listFiles()) {

if(file.isFile() file.getName().endsWith(".java")) {

int line = 0;

FileReader fr = new FileReader(file);

BufferedReader br = new BufferedReader(new FileReader(file));

String s = null ;

while ( (s=br.readLine()) != null) {

s = s.replaceAll("\\s", "");

if ("".equals(s) || s.startsWith("//")) {

} else {

System.out.println(s);

line ++;

}

}

bw.write(file.getName() + "\t\t" + line);

bw.newLine();

br.close();

fr.close();

sumFile ++;

sumLine += line;

}

}

bw.write("统计:" + sumFile + "个类\t" + sumLine + "行");

bw.close();

fw.close();

} catch (Exception e) {

e.printStackTrace();

}

}

你再稍微改改应该就可以了

java编程一般注释应占多少百分比?

一般占 30%-40%

也不是这样确定,

首先是要在程序的开头写上这段代码的作用,或者自己为什么要写着一段代码

逻辑性很强的地方加上注释,使读者不用在头脑中仿真代码的运行过程

比如在 if 语句 for 语句,要是用到了一些比较复杂的算法,还要写的更清晰一些

祝你学习愉快


当前名称:统计java代码注释率的简单介绍
本文链接:http://cdkjz.cn/article/hghpdj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220