ImportJava.io.BufferedReader;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileReader;Importjava.io.IOException; Public classCountcode {Static intNomalcount = 0;//Line of code Static intCommoncount = 0;//Comment Lines Static intWhitecount = 0;//Blank Line Public Static voidMain (string[] args) {File F=NewFile ("D:\\testjava"); file[] Files=F.listfiles (); for(inti=0;i<files.length;i++) {File ff=Files[i]; System.out.println (Ff.getname ()); if(Ff.getname (). Matches (". *\\.java$") {System.out.println ("000000"); Parse (FF); }} System.out.println ("Nomoalcount=" +nomalcount); System.out.println ("Commoncount=" +commoncount); System.out.println ("Whitecount=" +whitecount); } Private Static voidParse (File f) {BufferedReader br=NULL; BooleanFlag =false; Try{br=NewBufferedReader (NewFileReader (f)); String Line= ""; while(line = Br.readline ())! =NULL){
line = Line.trim (); if(Line.matches ("^[\\s&&[^\\n]]*$")) {//blank line Regular expressionwhitecount++; }Else if(Line.startswith ("/*") &&!line.endswith ("* *") ) {Commoncount++; Flag=true; }Else if(true==flag) {Commoncount++; if(Line.endswith ("*/") {System.out.println ("SDFSD"); Flag=false; } }Else{Nomalcount++; } } } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
Regular expression Statistics Java code blank line, valid code