×

Loading...

Dear Mr. Pig, just for your reference

The following is the code that may be useful to some people. My code is not polished. If you are a coding expert, please do not comment on the stupidness in my code. Thank you.

import java.io.*;

public class FileCount{

private static int javacount=0;

public static void main(String[] args) throws Exception{
if(args.length !=1) {
System.out.println("usge: java FileCount <dir>");
return;
}
File file = new File(args[0]);
count(file);
System.out.println("There are " + javacount + " java files under "+ args[0]);

}

private static void count(File f)throws Exception{
File list[] = f.listFiles();
for(int i=0; i<list.length; i++) {
if(list[i].isDirectory()) {
count(list[i]);
} else{
String s = list[i].getName();
int index = s.indexOf(".java");
if(index !=-1) {
javacount++;
}

}
}

}
Sign in and Reply Report

Replies, comments and Discussions: