求一个JAVA文件里面的代码行空行和注释行

一个JAVA文件,怎么才能知道他里面有多少空行多少注释行,多少代码行,假如该文件名为ToolTest.java,用代码写出来,急

package demo1;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * @ClassName: BufferedDemo
 * @Description: (获得Java文件中的空行和注释行,作为工具类)
 * @author  幸福源于点滴珍惜!
 * @author 砖尘
 * @date Mar 18, 2014 8:55:18 PM
 * 
 */
public class BufferedDemo {
private String path;
private BufferedReader bfr;
private int count;

public BufferedDemo(String path) throws FileNotFoundException {
this.path = path;
bfr = new BufferedReader(new FileReader(path));
}

public int getNullLine() throws IOException {// 获得空行
String len = "";
while ((len = bfr.readLine()) != null) {
if (len.equals(""))
count++;
}
return count;
}

public int getNotesLine() throws IOException {// 获取注释行,这边仅认为/***/
// 和/**/是注释。
String len = "";
while ((len = bfr.readLine()) != null) {
if (len.contains("/*")) {
if (len.contains("*/"))
count++;
else {
count += 2;
while (!(len = bfr.readLine()).contains("*/"))
count++;
}

}
}
return count;
}
}

 工具类中没有写主函数,要运行需要增加主函数。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-03-18
public static void main(String a[]) {
FileReader in = null; 
boolean comment=false;
int b = 0; 
int normalLines=0;
int commentLines=0;
try {
in=new FileReader("D:\\Main.java"); 
BufferedReader in1 = 
new BufferedReader(in); 
String s, s2 = new String(); 
while((s = in1.readLine()) != null) {
//s2 += s + "\n"; 
System.out.println(s); 
s=s.trim();
if(s.matches("^[\\s&&[^\\n]]*$")) {
     b++;
    }
else if (s.startsWith("/*")) {
     commentLines ++;
     comment = true; 
    } else if (true == comment) {
     commentLines ++;
     if(s.endsWith("*/")) {
      comment = false;//看清楚了,这个if写在哪了
     }
    } else if (s.startsWith("//")) {
     commentLines ++;
    } else {
     normalLines ++;
    }

}

in.close(); 

System.out.println("ok!"); 
} catch (FileNotFoundException e) { 
System.out.println("找不到文件"); 
System.exit(-1); 
e.printStackTrace(); 

catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("空白行:"+b); 
System.out.println("注释行:"+commentLines); 
System.out.println("代码:"+normalLines); 
}

第2个回答  2014-03-19
package com.lovoinfo.firstTeam;
import
javax.swing.*;

public class Loader {

public static void main(String[] args)
{
String filename =
JOptionPane.showInputDialog(null,"请输入文件路径:");
CountFile cf = new
CountFile(filename);

JOptionPane.showMessageDialog(null,cf.count());
}

}

package com.lovoinfo.firstTeam;

import java.io.*;

import javax.swing.*;

/**
* 统计JAVA文件中代码行数,注释行数
* @author
晏云波
*/
public class CountFile {
private String
str;

/**
* 构造器
* @param filename
要统计的文件名
*/
public CountFile(String filename)
{
try {
str =
fromFile(filename);
} catch (FileNotFoundException e)
{
JOptionPane.showMessageDialog(null,"该文件不存在或不可访问");
System.exit(0);
}
catch (IOException e) {
JOptionPane.showMessageDialog(null,"失败的或中断的 I/O
操作");
System.exit(0);
}

}

/**
* 统计
* @return 统计结果

*/
public String count() {
String strReturn =
"";
String strTemp = "";
int codeNumber =
0;
int remarkNumber = 0;

if (str.length() > 0)
{
while (str.indexOf('\n') != -1)
{
strTemp = str.substring(0,
str.indexOf('\n')).trim();
str =
str.substring(str.indexOf('\n') + 1,
str.length());

if
(strTemp.length() > 0 && (strTemp.charAt(0) == '*' ||
strTemp.charAt(0) == '/'))
{
remarkNumber++;
}
else
{
codeNumber++;
}
}

codeNumber++;
}
strReturn
= "代码" + codeNumber + "行," + "注释" + remarkNumber + "行";

return strReturn;
}

/**
* 读取文件
* @param filename
读取的文件名
* @return 返回文件的内容
* @throws FileNotFoundException
文件不存在或不可访问
* @throws IOException 失败的或中断的 I/O 操作

*/
private String fromFile(String filename) throws
FileNotFoundException,
IOException {
File
file = new File(filename);
FileInputStream fis = new
FileInputStream(file);

byte[] b = new byte[(int)
file.length()];

fis.read(b);
fis.close();

return
new String(b);
}

}本回答被网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网