Import Java. io. bufferedreader; import Java. io. file; import Java. io. filereader; import Java. util. arraylist; import Java. util. list; /*************************************** * ********** @ description Calculation Source code (SRC) number of rows, no blank line * purpose: assemble all files under SRC into a list, and then filter out the files, * @ author gumutianqi * @ date 2:00:12 * @ version 1.0 ********************* * *************************/public class linecounter {list <File> List = new arraylist <File> (); int linenumber = 0; filereader Fr = NULL; bufferedreader BR = NULL; Public void counter (string projectname) {// string Path = system. getproperty ("user. dir "); string Path = linecounter. class. getresource ("/"). getpath (); // same as the next Path = path. substring (0, path. length ()-24) + projectname + "/src"; system. out. println (PATH); file = new file (PATH); file files [] = NULL; Files = file. listfiles (); AddFile (files); isdirectory (files); readlineperfile (); system. out. println ("totle:" + linenumber + "row");} // determines whether the directory is public void isdirectory (file [] files) {for (FIL E s: Files) {If (S. isdirectory () {file [] = S. listfiles (); AddFile (File); isdirectory (File); Continue ;}}// organize all files under SRC into list public void AddFile (File file []) {for (INT Index = 0; index <file. length; index ++) {list. add (file [Index]); // system. out. println (list. size () ;}/// read non-blank rows public void readlineperfile () {try {for (File S: List) {int yuan = linenumber; If (S. isdirectory ()){ Continue;} Fr = new filereader (s); BR = new bufferedreader (FR); string I = ""; while (I = Br. Readline ())! = NULL) {If (isblankline (I) linenumber ++;} system. out. print (S. getname (); system. out. println ("\ t has" + (linenumber-yuan) + "row");} catch (exception e) {e. printstacktrace ();} finally {If (BR! = NULL) {try {Br. Close ();} catch (exception e) {}} if (fr! = NULL) {try {Fr. close () ;}catch (exception e) {}}// is the empty line public Boolean isblankline (string I) {if (I. trim (). length () = 0) {return false;} else {return true;} public static void main (string ARGs []) {linecounter lc = new linecounter (); string projectname = "testproject"; // enter your project name LC. counter (projectname );}}
Note: In the final main method, enter the name of the project you want to calculate.