当前位置:首页>开发>正文

idea统计java代码行数无插件 java如何统计文章的行数

2023-04-10 12:56:13 互联网 未知 开发

idea统计java代码行数无插件 java如何统计文章的行数

java如何统计文章的行数

import java.io.*
public class Test{
public static void main(String args[])throws Exception{
File file = new File("文件路径")//文件路径:例子:F:/myfile.txt或者F:\myfile.txt
BufferedReader br = new BufferedReader(new FileReader(file))//构造一个BufferedReader类来读取文件
int n=0//计数器
String s = null
while((s = br.readLine())!=null){//使用readLine方法,一次读一行
n
}
br.close()
System.out.println(n)//打印出行数
}
}

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

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


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

怎么用java统计一篇文章的行数

BufferedReader br=new BufferedReader(new FileReader("文件URL"))
int count = 0
String str=br.readLine()
while(str!=null){
count
str=br.readLine()
}
System.out.println("这篇文章共有" count "行")

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

有没有Eclipse的代码行数插件

放到eclipse插件目录,启动eclipse后,使用主菜单的“Window->Show View->Other->统计->代码行统计”打开统计视图,选中要统计的java包或文件,点击视图右.

最新文章