今天,有时间重新复习了下毕老师讲的正则表达式,想写个代码统计小程序,里面记录文件中空白行数、实际代码行数以及注释行数。输出的数据不正确,请教各位帮忙解决下,谢谢啊! 代码如下: 
 
- import java.io.BufferedReader;
 
 - import java.io.File;
 
 - import java.io.FileNotFoundException;
 
 - import java.io.FileReader;
 
 - import java.io.IOException;
 
 - public class CodeCounterTest {
 
 -         private static long whiteLines = 0; //记录Java文件中所有的空白行
 
 -         private static long normalLines = 0; //记录Java文件中所有的代码行
 
 -         private static long commentLines = 0; //记录Java文件中所有的注释行
 
 -         public static void main(String[] args) {
 
 -                 File file = new File("e:\\jad\\");
 
 -                 File[] codeFiles = file.listFiles();
 
 -                 if ((codeFiles != null) && (codeFiles.length>0)) {
 
 -                         
 
 -                         for (File f: codeFiles) {
 
 -                                 if (f.getName().matches(".*\\.java$")) {
 
 -                                         parse(f);
 
 -                                 }        
 
 -                         }
 
 -                 }        
 
 -                 System.out.println("whiteLines: "+whiteLines);
 
 -                 System.out.println("normalLines: "+normalLines);
 
 -                 System.out.println("commentLines: "+commentLines);
 
 -         }
 
  
-         private static void parse(File f) {
 
 -                 BufferedReader br = null;
 
 -                 String line = null;        
 
 -                 boolean noteFlag = false;
 
 -                 try {
 
 -                         br = new BufferedReader(new FileReader(f));
 
 -                         
 
 -                         while ((line = br.readLine()) != null) {
 
 -                                 if (line.matches("^[\\s]*$")) {
 
 -                                         whiteLines ++;
 
 -                                 } else if (line.startsWith("/*") && !line.endsWith("*/")) {
 
 -                                         noteFlag = true;
 
 -                                         commentLines ++;
 
 -                                         
 
 -                                 } else if (line.startsWith("/*") && line.endsWith("*/")) {
 
 -                                         commentLines ++;
 
 -                                 } else if (line.startsWith("//")) {
 
 -                                         commentLines ++;
 
 -                                 } else if (true == noteFlag) {
 
 -                                         commentLines ++;
 
 -                                         if (line.endsWith("*/")) {
 
 -                                                 noteFlag = false;
 
 -                                         }
 
 -                                 } 
 
 -                                 else {
 
 -                                         normalLines ++;
 
 -                                 }
 
 -                         }
 
 -                 } catch (FileNotFoundException e) {
 
 -                         e.printStackTrace();
 
 -                 } catch (IOException e) {
 
 -                         e.printStackTrace();
 
 -                 } finally {
 
 -                         if (br != null) {
 
 -                                 try {
 
 -                                         br.close();
 
 -                                         br = null;
 
 -                                 } catch (IOException e) {
 
 -                                         e.printStackTrace();
 
 -                                 }
 
 -                         }
 
 -                 }
 
 -         }
 
 - }
 
 
  复制代码 |