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)