导航:首页 > 编程语言 > java代码行数统计

java代码行数统计

发布时间:2022-06-11 15:34:23

java统计类中物理总行数中注释行,非注释行,以及代码的行数

import java.io.*;public class Check {
public static void main (String[] args) throws IOException{
check("D:/MyEclipse/java/src/my/Check.java");//这里的字符串是你要统计的文件的路径,你自己填写
} public static void check (String s) throws IOException{
int all = 0, empty = 0, describe = -1, i = 0;
String str = null;
File f = new File(s);
BufferedReader br = new BufferedReader (new FileReader(f));
str = br.readLine();
while(str != null){
all++;
if(str.trim().equals("")) empty++;
if(str.contains("//")) describe++;
if(str.contains("/*")){
while(!str.contains("*/")){
i++;
all++;
describe++;
str = br.readLine();
}
}
str = br.readLine();
}
System.out.println("文件物理总行数为:" + all);//;;klj
System.out.println("文件中空行数为:" + empty);//hkk
System.out.println("文件注释行数为:" + describe);
System.out.println("文件非注释行数为:" + (all - i));
/*asdfdsff
* sdasadfsf//fg
* asdfsdf//dsfg
* asdf
*/
}
}以上是代码,我在我的机子上实现了,希望能帮到你!我也是JAVA菜鸟,希望有高手能更好地解答

Ⅱ eclipse怎么统计代码行数

步骤如下:

1、打开File Search对话框。
2、选中正则表达式,在搜索文本框输入\n 。
3、文件名称输入 *.java。
4、在范围里选中Enclosing projects。
经过上面方式,就可以统计出整个项目的代码行数。

Ⅲ 求问怎么统计JAVA代码行数有什么工具

源代码行数统计器 1.5
本软件用于统计软件工程源代码行数,
可对指定的子目录下或整个目录树中所有指定类型的源代码文件进行行数统计。

Ⅳ 如何统计某目录下的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);
}
}

Ⅵ 输入:1个目录 输出:这个目录下的java文件的代码行数总和(统计时忽视注释和空行),

//统计程序生成文件的行数
package package_1;
import java.io.*;
public class Linknum {
File tempFile1,tempFile2;
File tempFiles[] = null;
String path,temp = "";
int num = 2;
public Linknum() throws IOException{
tempFile1 = new File("");
path = tempFile1.getAbsolutePath() + "\\src\\package_1";
tempFile2 = new File(path);
tempFiles = tempFile2.listFiles();
if(tempFiles != null){
for(int i = 0;i<tempFiles.length;i++){
if(tempFiles[i].toString().equals(path + "\\Linknum.java")){
FileReader fr = new FileReader(tempFiles[i]);
BufferedReader br = new BufferedReader(fr);
String temp2 = br.readLine();
temp = 1 + "\t" + temp + temp2 + "\r\n" + num + "\t";
while((temp2 = br.readLine()) != null){
num++;
temp = temp + temp2 + "\r\n" + num + "\t";
}
br.close();
fr.close();
}
}
FileWriter fw = new FileWriter(new File(path,"Count.ljl"));
BufferedWriter bw = new BufferedWriter(fw);
bw.write(temp);
bw.close();
fw.close();
}
}
public Linknum(String s)throws IOException{
tempFile1 = new File("");
path = tempFile1.getAbsolutePath() + "\\src\\package_1";
tempFile2 = new File(path);
tempFiles = tempFile2.listFiles();
if(tempFiles != null){
for(int i = 0;i<tempFiles.length;i++){
if(tempFiles[i].toString().equals(path + "\\Linknum.java")){
FileReader fr = new FileReader(tempFiles[i]);
BufferedReader br = new BufferedReader(fr);
String temp2 = br.readLine();
temp = 1 + ". " + temp + temp2 + "\r\n" + num + ". ";
while((temp2 = br.readLine()) != null){
num++;
temp = temp + temp2 + "\r\n" + num + ". ";
}
br.close();
fr.close();
}
}
FileWriter fw = new FileWriter(new File(s,"Count.ljl"));
BufferedWriter bw = new BufferedWriter(fw);
bw.write(temp);
bw.close();
fw.close();
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入保存文件的路径(或输入空格默认):");
String str = br.readLine();
br.close();
if(str.equals(" "))
new Linknum();
else
new Linknum(str);
System.out.println("行数统计完毕,请查看。");
}
}
差不多,自己改改吧,我这个不能忽略注释和空行。

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

方法一:
如果想要通过java代码的方式来计算.java文件的行数,可以通过IO来读取,
BufferedReader的方法readLine()来按行读取,每读取一行,行数+1
方法二:
如果要查看.java文件的代码行数,
可以使用现成的IDE工具,比如ECLIPSE...
每一行的行号都有表示出来

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

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

Ⅸ java中怎么获得一个文本文件的行数

涉及到java中读写文件的IO操作。
获取一个文本文件的行数较为方便的方法,是通过BufferedReader类的readLine()方法,间接的统计行数。
源代码:
public
static
int
getTextLines()
throws
IOException
{
String
path
=
"c:\\job.txt"
;//
定义文件路径
FileReader
fr
=
new
FileReader(path);
//这里定义一个字符流的输入流的节点流,用于读取文件(一个字符一个字符的读取)
BufferedReader
br
=
new
BufferedReader(fr);
//
在定义好的流基础上套接一个处理流,用于更加效率的读取文件(一行一行的读取)
int
x
=
0;
//
用于统计行数,从0开始
while(br.readLine()
!=
null)
{
//
readLine()方法是按行读的,返回值是这行的内容
x++;
//
每读一行,则变量x累加1
}
return
x;
//返回总的行数
}

阅读全文

与java代码行数统计相关的资料

热点内容
会日语的程序员 浏览:17
网银密码加密失败怎么回事 浏览:725
android开发音乐播放器 浏览:806
ug120阵列命令快捷键 浏览:597
气动隔膜式压缩机 浏览:470
linux如何修改主机名 浏览:104
单片机光标上下移动 浏览:528
数据加密验证 浏览:108
程序员被激怒 浏览:891
winxp找不到服务器dns地址 浏览:842
以文本文件的格式保存考生文件夹 浏览:41
编译原理文法分为几类 浏览:570
JAVA基础学python要多久 浏览:74
java流量控制 浏览:936
java实现多重继承 浏览:707
票据通加密狗怎么在新系统上使用 浏览:795
航模加密狗连接电脑 浏览:473
好用的汇编语言编译器 浏览:863
自己编译安卓虚拟机 浏览:913
中国的古代算法 浏览:656