package com.nsj.tool.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class CodeCounter {
static long normalLines = 0;
static long commentLines = 0;
static long blankLines = 0;
public static void main(String[] args) {
File f = new File("D://D//JavaWork//http//httpclientdemo//src");
getFiles(f);
System.out.println("SUM CODELINE:" + normalLines);
System.out.println("SUM COMMENTLINE:" + commentLines);
System.out.println("SUM BLANKLINE:" + blankLines);
}
private static void getFiles(File f) {
File[] codeFiles = f.listFiles();
for (int i = 0; i < codeFiles.length; i++) {
if (codeFiles[i].isDirectory()) {
getFiles(codeFiles[i]);
} else if (codeFiles[i].getName().matches(".*//.java$")) {
System.out.println(codeFiles[i].getName());
parse_java(codeFiles[i]);
}
}
}
private static void parse_java(File f) {
long normalLines1 = 0;
long commentLines1 = 0;
long blankLines1 = 0;
BufferedReader br = null;
boolean comment = false;
try {
br = new BufferedReader(new FileReader(f));
String line = "";
while ((line = br.readLine()) != null) {
line = line.trim();
//line = line.replaceAll(" ", "");
if (line.matches("^[[//s]&&[^//n]]*$")) { // spaceLine ?*$
blankLines1++;
} else if (line.startsWith("//")
|| (line.startsWith("/*") && line.endsWith("*/"))) {
commentLines1++;
} else if (line.startsWith("/*") && !line.endsWith("*/")) {
commentLines1++;
comment = true;
} else if ((line.endsWith("*/")) && comment == true) {
commentLines1++;
comment = false;
} else if (line.endsWith("*/")) {
commentLines1++;
comment = false;
} else if (comment == true) {
commentLines1++;
} else {
normalLines1++;
}
}
System.out.println(f.getName() + " CODELINE: " + normalLines1);
System.out.println(f.getName() + " COMMENTLINE: " + commentLines1);
System.out.println(f.getName() + " BLANKLINE: " + blankLines1);
normalLines += normalLines1;
commentLines += commentLines1;
blankLines += blankLines1;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static void parse_vb(File f) {
long normalLines1 = 0;
long commentLines1 = 0;
long blankLines1 = 0;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(f));
String line = "";
while ((line = br.readLine()) != null) {
line = line.trim();
//line = line.replaceAll(" ", "");
if (line.matches("^[[//s]&&[^//n]]*$")) { // spaceLine ?*$
blankLines1++;
} else if (line.startsWith("'")) {
commentLines1++;
} else {
normalLines1++;
}
}
System.out.println(f.getName() + " CODELINE: " + normalLines1);
System.out.println(f.getName() + " COMMENTLINE: " + commentLines1);
System.out.println(f.getName() + " BLANKLINE: " + blankLines1);
normalLines += normalLines1;
commentLines += commentLines1;
blankLines += blankLines1;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}