资讯

精准传达 • 有效沟通

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

java代码行数统计,java代码行数统计工具

如何计算一个.java文件的代码行数

方法一:

创新互联公司服务项目包括平塘网站建设、平塘网站制作、平塘网页制作以及平塘网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,平塘网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到平塘省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

如果想要通过java代码的方式来计算.java文件的行数,可以通过IO来读取,

BufferedReader的方法readLine()来按行读取,每读取一行,行数+1

方法二:

如果要查看.java文件的代码行数,

可以使用现成的IDE工具,比如ECLIPSE...

每一行的行号都有表示出来

eclipse怎么统计代码行数

步骤如下:

1、打开File Search对话框。

2、选中正则表达式,在搜索文本框输入\n 。

3、文件名称输入 *.java。

4、在范围里选中Enclosing projects。

经过上面方式,就可以统计出整个项目的代码行数。

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

}

}

anaconda如何计算全部代码行数

计算全部代码行数方法。

方法一:在需要统计的目录下执行(直接复制可能会有字符编码问题,建议手撸,如需统计其他形式文件行数,只需修改.java为相应文件后缀即可)

find.-name*.java-execwc-l{};|awk‘{s+=$1}END{prints}’

方法二:l/传入文件路径返回文件下所有文件内容行数unsignedlongcodeLineCount(NSString*Path)

//获得文件管理者

NSFileManager*mgr=[NSFileManagerdefaultManager];

//判path是文件夹还是文件路径

BoOLdir=No;//标记是否为文件夹

l/这个路径是否存在

BoOLexists=[mgrfileExistsAtPath:PathisDirectory:dir];

//如果不存在,直接返回e

if(!exists)returne;

if(dir){

NSLog(@"是个文件夹");

怎么用java编写统计文件中的字符数、单词数和行数?

在C盘新建文件1.txt,输入任意字符,如下图:

编写java代码。如下:

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.TreeMap;

public class Test {

// 统计数字或者字符出现的次数

public static TreeMapCharacter, Integer Pross(String str) {

char[] charArray = str.toCharArray();

TreeMapCharacter, Integer tm = new TreeMapCharacter, Integer();

for (int x = 0; x  charArray.length; x++) {

if (!tm.containsKey(charArray[x])) {

tm.put(charArray[x], 1);

} else {

int count = tm.get(charArray[x]) + 1;

tm.put(charArray[x], count);

}

}

return tm;

}

public static void main(String[] args) {

BufferedReader br = null;

int line = 0;

String str = "";

StringBuffer sb  = new StringBuffer();

try {

br = new BufferedReader(new FileReader("c:\\1.txt"));

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

sb.append(str);

++line;

}

System.out.println("\n文件行数: " + line);

System.out.println("\n文件内容: " + sb.toString());

TreeMapCharacter, Integer tm = Pross(sb.toString());

System.out.println("\n字符统计结果为:" + tm);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (br != null) {

try {

br.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}运行结果如下图:


当前标题:java代码行数统计,java代码行数统计工具
网站URL:http://cdkjz.cn/article/dsshcsp.html
多年建站经验

多一份参考,总有益处

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

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

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