Skip to content

Instantly share code, notes, and snippets.

@mainlove
Created August 23, 2013 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mainlove/6316882 to your computer and use it in GitHub Desktop.
Save mainlove/6316882 to your computer and use it in GitHub Desktop.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class CodeCount {
public static void main(String[] args) throws FileNotFoundException {
if (args.length == 0) {
System.out.println("==> Usage: java com.ebao.misc.CodeCount [directory]");
System.exit(1);
}
File basedir = new File(args[0]);
if (!basedir.isDirectory()) {
System.out.println("==> Error: " + args[0] + " is not a directory");
System.exit(1);
}
Lookup(basedir);
}
private static void Lookup(File dir) throws FileNotFoundException {
File[] names = dir.listFiles();
for (File name : names) {
if (name.isFile()) {
int lineCount = Count(name);
System.out.printf("%20s%10d\n", name.getName(), lineCount);
} else if (name.isDirectory()) {
Lookup(name);
} else {
System.out.println("==> Error: " + name.getName() + " is invalid");
System.exit(1);
}
}
}
/*
/*
* @param file
* @return
* @throws FileNotFoundException
*/
private static int Count(File file) throws FileNotFoundException {
Scanner in = new Scanner(file);
String line = null;
boolean commentMode = false;
int lineCount = 0;
while (in.hasNextLine()) {
line = in.nextLine().trim();
if (line.startsWith("//") || line.equals("")) {
continue;
}
if (line.startsWith("/*")) {
commentMode = true;
}
if (commentMode && line.endsWith("*/")) {
commentMode = false;
continue;
}
if (!commentMode)
++lineCount;
}
in.close();
return lineCount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment