正则表达式统计java代码空白行,有效代码
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class CountCode { static int nomalCount = 0;//代码行 static int commonCount = 0;//注释行 static int whiteCount = 0;//空白行 public static void main(String[] args) { File f = new File("D:\\testjava"); File[] files = f.listFiles(); for(int i=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 void parse(File f) { BufferedReader br = null; boolean flag = false; try { br = new BufferedReader(new FileReader(f)); String line = ""; while((line = br.readLine()) != null){
line = line.trim(); if(line.matches("^[\\s&&[^\\n]]*$")){//空白行正则表达式 whiteCount++; }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 block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。