下面的程序统计小一点的文件,没问题.
可是统计整个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();
}
}
可是统计整个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();
}
}
public CodeCounter(String s) {
File f = new File(s);
tree(f);
}
这段修改一下看看,现在我没时间帮你调了,自己baidu查一下试试吧
你用的是fat32么?
不行就捕获一下,把异常处理了.
就可以了你试一下
就可以了你试一下"
D:,这样不行呀,直接就
normalLines: 0
commentLines: 0
whiteLines: 0我的D盘是NTFS的开始的时候,程序在运行(能听见硬盘响声,大约半分钟,就抛异常了)
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();返回为空
所以有 控指针异常 判断一下
空文件夹怎么判断的呀?
我下面弄的不对呀!
结果:
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]);
}
}
}
刚才试了下,"空指针异常"确实是空文件夹引起的呀!!
可是空文件夹怎么排除呀?
我上面写的那个不对呢?
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]);
}
}
}
是这个文件过不去的,"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]);
}
}
}
return;
}
有了这句话在
File[] childs = f.listFiles();
后面不就可以了
嘿嘿,我那个equals效率太差了