Java Code Line statistics

Source: Internet
Author: User

 PackageCodecounter;ImportJava.io.BufferedReader;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileReader;Importjava.io.IOException;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern; Public classCodecounter {Static LongCommentline = 0; Static LongWhiteline = 0; Static LongNormalline = 0; Static LongTotalline = 0; Static BooleanComment =false; Static FinalString Base_dir = "D:\\JAVAP\\QDGS\\QDGS_GZFW\\SRC\\JAVA\\QDGSGZFW";  Public Static voidMain (string[] args) {File file=NewFile (Base_dir);//Enter the folder path to be counted heregetchild (file); System.out.println ("Valid lines of code:" +normalline); System.out.println (Number of comment lines: +commentline); System.out.println ("Blank line number:" +whiteline); System.out.println ("Total lines of code:" +totalline); }        Private Static voidGetchild (File child) {//Traversing subdirectories        if(Child.getname (). Matches (". *\\.java$")) {//querying only Java files            Try{bufferedreader br=NewBufferedReader (NewFileReader (child)); String Line= "";  while(line = Br.readline ())! =NULL) {parse (line); }              } Catch(FileNotFoundException E1) {e1.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }          }          if(Child.listfiles ()! =NULL) {               for(File f:child.listfiles ()) {getchild (f); }          }      }        Private Static voidParse (String line) { line=Line.trim (); Totalline++; if(line.length () = = 0) {Whiteline++; } Else if(comment) {commentline++; if(Line.endswith ("*/") ) {Comment=false; } Else if(Line.matches (". *\\*/.+") ) {Normalline++; Comment=false; }          } Else if(Line.startswith ("//") ) {Commentline++; } Else if(Line.matches (". +//.*") ) {Commentline++; Normalline++; } Else if(Line.startswith ("/*") && line.matches (". +\\*/.+")) ) {Commentline++; Normalline++; if(Findpair (line)) {Comment=false; } Else{Comment=true; }          } Else if(Line.startswith ("/*") &&!line.endswith ("* *") ) {Commentline++; Comment=true; } Else if(Line.startswith ("/*") && line.endswith ("* *") ) {Commentline++; Comment=false; } Else if(Line.matches (". +/\\*.*") &&!line.endswith ("* *") ) {Commentline++; Normalline++; if(Findpair (line)) {Comment=false; } Else{Comment=true; }          } Else if(Line.matches (". +/\\*.*") && line.endswith ("* *") ) {Commentline++; Normalline++; Comment=false; } Else{normalline++; }      }        Private Static BooleanFindpair (String line) {//find a row in the/* and */whether paired appears        intCount1 = 0; intCount2 = 0; Pattern P= Pattern.compile ("/\\*"); Matcher m=P.matcher (line);  while(M.find ()) {Count1++; } P= Pattern.compile ("\\*/"); M=P.matcher (line);  while(M.find ()) {Count2++; }          return(Count1 = =Count2); }  }

Java Code Line statistics (RPM)

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.