Java code line statistics Tool

Source: Internet
Author: User

Import Java. io. bufferedreader; <br/> Import Java. io. file; <br/> Import Java. io. filenotfoundexception; <br/> Import Java. io. filereader; <br/> Import Java. io. ioexception; </P> <p>/** <br/> * <SPAN class = 'wp _ keywordlink '> Code </span> row count Statistics <br/> * @ author fog non-fog <br/> * @ date 2011-05-25 <br/> */<br/> public class statisticcodelines {</P> <p> Public static int normallines = 0; // valid <SPAN class = 'wp _ keywordlink '> Program </span> Number of rows <br/> Public static int whitelines = 0; // Number of blank rows <br/> Public static int commentlines = 0; // Number of comment rows </P> <p> Public static void main (string [] ARGs) throws ioexception {</P> <p> file = new file ("D: // workspace // project "); <br/> If (file. exists () {<br/> statistic (File); <br/>}< br/> system. out. println ("total valid code lines:" + normallines); <br/> system. out. println ("Total number of blank rows:" + whitelines); <br/> system. out. println ("Total comment Number of rows: "+ commentlines); <br/> system. out. println ("Total number of rows:" + (normallines + whitelines + commentlines); <br/>}</P> <p> Private Static void statistic (File file) throws ioexception {</P> <p> If (file. isdirectory () {<br/> file [] files = file. listfiles (); <br/> for (INT I = 0; I <files. length; I ++) {<br/> statistic (files [I]); <br/>}< br/> If (file. isfile () {<br/> // file with the statistic extension of Java <br/> If (file. getname (). Matches (". *//. java ") {<br/> parse (File ); <br/>}</P> <p> Public static void parse (File file) {<br/> bufferedreader BR = NULL; <br/> // determines whether the row is a comment row <br/> Boolean comment = false; <br/> int temp_whitelines = 0; <br/> int temp_commentlines = 0; <br/> int temp_normallines = 0; </P> <p> try {<br/> Br = new bufferedreader (New filereader (File); <br/> string line = ""; <br/> while (line = BR. readline () )! = NULL) {<br/> line = line. trim (); <br/> If (line. matches ("^ [// S & [^/n] * $") {<br/> // empty rows <br/> whitelines ++; <br/> temp_whitelines ++; <br/>} else if (line. startswith ("/*")&&! Line. endswith ("*/") {<br/> // commentlines starting with "/*" <br/> commentlines ++; <br/> comment = true; <br/>} else if (comment = true &&! Line. endswith ("*/") {<br/> // a row in a multi-line comment (not the beginning or end) <br/> commentlines ++; <br/> temp_commentlines ++; <br/>} else if (comment = true & line. endswith ("*/") {<br/> // end line of multi-line comment <br/> commentlines ++; <br/> temp_commentlines ++; <br/> comment = false; <br/>} else if (line. startswith ("//") {<br/> // comment row <br/> commentlines ++; <br/> temp_commentlines ++; <br/>} else {<br/> // normal code line <br/> normallines ++; <br/> temp _ Normallines ++; <br/>}</P> <p> system. out. println ("valid rows" + temp_normallines + <br/> ", blank rows" + temp_whitelines + <br/> ", comment rows" + temp_commentlines + <br/> ", total number of rows "+ (temp_normallines + temp_whitelines + temp_commentlines) + <br/>" + file. getname (); </P> <p>} catch (filenotfoundexception e) {<br/> E. printstacktrace (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}finally {<Br/> If (BR! = NULL) {<br/> try {<br/> Br. close (); <br/> Br = NULL; <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}</P> <p>}

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.