Tool-class code, obtain the total number of lines of all java files in the current project, the number of lines of code, the number of comments, and the number of blank lines
The code copy is as follows:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author Administrator
*
*/
public class JavaCode {
private static final String PROJECT_DIR = "C://test";
private static int totle = 0;//Total number of rows
private static int source = 0;//Number of code lines
private static int blank = 0;//Number of blank lines
private static int comments = 0;//Number of comments
/**
* Read java files in folder
* @param dir
*/
private static void listNext(File dir) {
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
//Discern whether it is a folder. If it is a folder, continue to search downward
if (files[i].isDirectory()) {
listNext(files[i]);
} else {
try {
if (files[i].getName().endsWith(".java")) {
System.out.println(files[i].getAbsolutePath());
javaLine(files[i]);
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* Read the total number of lines of java file, number of lines of code, number of blank lines, number of comment lines
* @param f
* @throws IOException
* @throws FileNotFoundException
*/
private static void javaLine(File f) throws FileNotFoundException, IOException{
String strLine = "";
String str = fromFile(f);
if (str.length() > 0) {
while (str.indexOf('/n') != -1) {
totle++;
//System.out.println(totle);
strLine = str.substring(0, str.indexOf('/n')).trim();
//str = str.substring(str.indexOf('/n') + 1, str.length());
if (strLine.length() == 0 ) {
blank++;
}else if (strLine.charAt(0) == '*' || strLine.charAt(0) == '/') {
comments++;
}else{
source++;
String regEx = "^*//";
if(regEx(strLine,regEx)){
comments++;
}
}
str = str.substring(str.indexOf('/n') + 1, str.length());
}
}
}
/**
* Read the java file as a character array
* @param f
* @return
* @throws FileNotFoundException
* @throws IOException
*/
private static String fromFile(File f) throws FileNotFoundException,IOException {
FileInputStream fis = new FileInputStream(f);
byte[] b = new byte[(int) f.length()];
fis.read(b);
fis.close();
return new String(b);
}
/**
* Regular match
* @param str Enter string
* @param regEx Regular matching string
* @return
*/
private static boolean regEx(String str,String regEx){
Pattern p=Pattern.compile(regEx);
Matcher m=p.matcher(str);
boolean result=m.find();
return result;
}
public static void main(String[] args) throws FileNotFoundException, IOException {
//File root = new File(PROJECT_DIR);
File directory = new File("");//The parameter is empty
//Get the project path
String projectFile = directory.getCanonicalPath();
System.out.println(projectFile+"=================================================);
listNext(new File(projectFile));
System.out.println(totle+1);
System.out.println(source);
System.out.println(blank);
System.out.println(comments);
}
}