Send a Java code for counting the number of project code lines

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.