求助:代码统计量小程序,我实在没辙了!!

yes152 2008-01-12 08:39:58
下面的程序统计小一点的文件,没问题.
可是统计整个D盘,就不行了,出现空指针异常...
Exception in thread "main" java.lang.NullPointerException
at CodeCounter.tree(TestCodeCounter.java:15)
at CodeCounter.tree(TestCodeCounter.java:19)
at CodeCounter.<init>(TestCodeCounter.java:10)
at TestCodeCounter.main(TestCodeCounter.java:74)


我真的没辙了!!求助下CSDN的朋友,看看那里需要该该呢?
十分感谢!!

import java.io.*;

class CodeCounter {
private static long normalLines = 0; //普通行
private static long commentLines = 0; //注释行
private static long whiteLines = 0; //空白行

public CodeCounter(String s) {
File f = new File(s);
tree(f);
}

public void tree(File f) {
File[] childs = f.listFiles();
for(int i = 0; i < childs.length; i++) {
if(childs[i].getName().matches(".*\\.java$")) {
parse(childs[i]);
}else if(childs[i].isDirectory()) {
tree(childs[i]);
}
}
}

public void parse(File f) {
BufferedReader br = null;
boolean comment = false;
try {
br = new BufferedReader(new FileReader(f));
String line = "";
while((line = br.readLine()) != null) {
line = line.trim();
if(line.matches("^[\\s&&[^\\n]]*$")) {
whiteLines ++;
}else if(line.startsWith("/*") && !line.endsWith("*/")) {
commentLines ++;
comment = true;
}else if(true == comment) {
commentLines ++;
if(line.endsWith("*/")) {
comment = false;
}
}else if(line.startsWith("//")) {
commentLines ++;
}else {
normalLines ++;
}
}
} 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 void p() {
System.out.println("normalLines: " + normalLines);
System.out.println("commentLines: " + commentLines);
System.out.println("whiteLines: " + whiteLines);
}

}
public class TestCodeCounter {
public static void main(String[] args) {
         //CodeCounter c = new CodeCounter("D:\\java"); //统计这个文件夹没问题
CodeCounter c = new CodeCounter("D:\\"); //整个D盘,就over了!!
c.p();
}
}
...全文
159 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
海会圣贤 2008-01-13
  • 打赏
  • 举报
回复
读一个文件还试过,但一个盘只怕不行了
yes152 2008-01-13
  • 打赏
  • 举报
回复
回复:sun_3211
空文件夹怎么判断的呀?
我下面弄的不对呀!
结果:
normalLines: 0
commentLines: 0
whiteLines: 0


	public void tree(File f) {
File[] childs = f.listFiles();
for(int i = 0; i < childs.length; i++) {
if((childs[i].isFile()) && childs[i].getName().matches(".*\\.java$")) {
parse(childs[i]);
}else if((childs[i].length() != 0) && childs[i].isDirectory()) {
tree(childs[i]);
}
}
}
yes152 2008-01-13
  • 打赏
  • 举报
回复
sun_3211的方法好呀!!
嘿嘿,我那个equals效率太差了
sun_3211 2008-01-13
  • 打赏
  • 举报
回复
if(childs ==null){
return;
}

有了这句话在
File[] childs = f.listFiles();
后面不就可以了
yes152 2008-01-13
  • 打赏
  • 举报
回复
回复:wangqiyy
是这个文件过不去的,"System Volume Information".D:\System Volume Information

我把它排除了,这次过去了呀.哈哈,你的这句话真管用呀(System.out.println("Path : "+f.getPath());)!!学习了..
public void tree(File f) {
System.out.println("Path : " + f.getPath());
File[] childs = f.listFiles();
for(int i = 0; i < childs.length; i++) {
if(childs[i].isDirectory()) {
if(childs[i].getName().equals("System Volume Information")) {
continue;
}else {
tree(childs[i]);
}

}else if(childs[i].getName().matches(".*\\.java$")) {
parse(childs[i]);
}
}
}


wangqiyy 2008-01-13
  • 打赏
  • 举报
回复
上面打错个字“for循环的时候如果没有内容就会过的。”
wangqiyy 2008-01-13
  • 打赏
  • 举报
回复
把tree的判断简单修改了一下,不用判断什么目录空不空,for循环的时候如果没有内容就会调过的。

public void tree(File f) {
System.out.println("Path : "+f.getPath());
File[] childs = f.listFiles();
for(int i = 0; i < childs.length; i++) {//这里决定了空目录不会执行
if(childs[i].isDirectory()){//这里决定什么是目录
tree(childs[i]);
}else if(childs[i].getName().matches(".*\\.java$")) {//原来这个判断是在前面的,如果正好有个.java的目录你就完蛋了
parse(childs[i]);
}
}
}
yes152 2008-01-13
  • 打赏
  • 举报
回复
??????????
yes152 2008-01-13
  • 打赏
  • 举报
回复
回复:sun_3211
刚才试了下,"空指针异常"确实是空文件夹引起的呀!!
可是空文件夹怎么排除呀?
我上面写的那个不对呢?
yes152 2008-01-13
  • 打赏
  • 举报
回复
谢谢大家了
  • 打赏
  • 举报
回复
哦,学习了。。。
sun_3211 2008-01-12
  • 打赏
  • 举报
回复
File[] childs = f.listFiles();
for(int i = 0; i < childs.length; i++) {
if(childs[i].getName().matches(".*\\.java$")) {
parse(childs[i]);
}else if(childs[i].isDirectory()) {
tree(childs[i]);
}
}

我想起来了 当 有个空文件夹的时候
File[] childs = f.listFiles();返回为空
所以有 控指针异常 判断一下
yes152 2008-01-12
  • 打赏
  • 举报
回复
"CodeCounter c = new CodeCounter("D:");
就可以了你试一下"
D:,这样不行呀,直接就
normalLines: 0
commentLines: 0
whiteLines: 0

我的D盘是NTFS的

开始的时候,程序在运行(能听见硬盘响声,大约半分钟,就抛异常了)
  • 打赏
  • 举报
回复
D盘,直接写成D:就可以了,再试试看。
sun_3211 2008-01-12
  • 打赏
  • 举报
回复
CodeCounter c = new CodeCounter("D:");
就可以了你试一下
  • 打赏
  • 举报
回复
我的那些个代码你可能要根据你的要求自行改一下吧,Java中的注释挺麻烦的。

我的那个代码能处理的注释符号,如://、/*等不能出现代码中的字符串里(这个处理还没有完成)。

你的代码的错误好像在tree()方法中。
约翰羊 2008-01-12
  • 打赏
  • 举报
回复
有可能是跟文件夹的权限有关系
你用的是fat32么?
不行就捕获一下,把异常处理了.
zhengjianbo4 2008-01-12
  • 打赏
  • 举报
回复
File不是针对文件的吗,可能你把
public CodeCounter(String s) {
File f = new File(s);
tree(f);
}
这段修改一下看看,现在我没时间帮你调了,自己baidu查一下试试吧
  • 打赏
  • 举报
回复
我前几天在下面的这张帖里贴过一个,你可以去参考一下。

http://topic.csdn.net/u/20080110/14/7ca0e8ec-18ff-410f-8300-32540b4e905d.html

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧