重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java显示代码行数 java如何显示行数

怎样把一个java代码中的有效代码行数,空行数,注释的行数分别打印出来 求高人指点呀

基本的模式已经给你了,你可以自己变通修改下,要有这方面的能力

创新互联为您提适合企业的网站设计 让您的网站在搜索引擎具有高度排名,让您的网站具备超强的网络竞争力!结合企业自身,进行网站设计及把握,最后结合企业文化和具体宗旨等,才能创作出一份性化解决方案。从网站策划到成都网站设计、网站建设, 我们的网页设计师为您提供的解决方案。

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class Admin {

public static void main(String... args) {

try {

Scanner in = new Scanner(new File("D:/gao.java"));

int nulSum = 0;

int zhushiSum = 0;

while (in.hasNext()) {

String str = in.nextLine().trim();

if ("".equals(str)) {

nulSum++;

}

if (str.startsWith("//")) {

zhushiSum++;

}

}

System.out.println("空行:" + nulSum);

System.out.println("注释:" + zhushiSum);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

}

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文件的代码行数

方法一:

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

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

方法二:

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

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

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

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 eclipse如何显示行数

问题1:在编辑区最左边地方右键,选择“Show Line Numbers”就行了。

问题2:快捷键(ctrl+f)

问题3:在工程名上右键,选择“Refactor-Rename”。

希望对你有帮助!


当前题目:java显示代码行数 java如何显示行数
URL链接:http://cqcxhl.cn/article/hhpcee.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP