如何统计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("代码行数统计:" + 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编写统计文件中的字符数、单词数和行数?

public class Test {\x0d\x0a\x0d\x0a public static void main(String[] args) throws Exception{\x0d\x0a Scanner input=new Scanner(System.in);\x0d\x0a System.out.println("请输入路径");\x0d\x0a String path=input.next();\x0d\x0a int charNum= 0 ;\x0d\x0a int wordsNum= 0;\x0d\x0a int lineNum = 0;\x0d\x0a InputStreamReader isr = new InputStreamReader(new FileInputStream(path)); \x0d\x0a BufferedReader br = new BufferedReader(isr);\x0d\x0a while( br.read()!= -1){\x0d\x0a String s = br.readLine();\x0d\x0a charNum+=s.length();\x0d\x0a wordsNum +=s.split(" ").length;\x0d\x0a lineNum ++; \x0d\x0a }\x0d\x0a isr.close();//关闭\x0d\x0a System.out.println("字符数:"+charNum+"\t单词数:"+wordsNum+"行 数:"+lineNum); \x0d\x0a\x0d\x0a }\x0d\x0a}

如何统计某目录下的java文件代码行数

可以自己写一个小程序,遍历每个文件;

如果是*.java就记录该文件的行数,依次累加。


网页标题:如何统计java代码行数 如何统计java代码行数的个数
当前路径:http://scyanting.com/article/dodepso.html