Tool class code to get all the total number of Java files in the current project, lines of code, number of lines of comments, number of blank lines
Copy Code code 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; Lines of code
private static int blank = 0; Number of Blank lines
private static int comments = 0; Comment Number of lines
/**
* 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++) {
Determine if the folder is a folder, and if it is, continue searching down
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 rows in the Java file, 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 Input string
* @param regEx Regular Match 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 ("");//parameter is empty
Get 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);
}
}