黑马程序员技术交流社区

标题: 给大家发一个代码统计的程序源码,看自己一天打多行 [打印本页]

作者: 刘圣伟    时间: 2012-8-11 18:30
标题: 给大家发一个代码统计的程序源码,看自己一天打多行
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.IOException;

  6. public class CodeCounter {
  7.        
  8.         static long normalLines = 0;
  9.         static long commentLines = 0;
  10.         static long whiteLines = 0;
  11.        
  12.         public static void main(String[] args) {
  13.                 //需要统计的代码放置的文件夹
  14.                 File f = new File("D:\\JavaProjects\\day11");
  15.                 File[] codeFiles = f.listFiles();
  16.                 for(File child : codeFiles){
  17.                         if(child.getName().matches(".*\\.java$")) {
  18.                                 parse(child);
  19.                         }
  20.                 }
  21.                
  22.                 System.out.println("normalLines:" + normalLines);
  23.                 System.out.println("commentLines:" + commentLines);
  24.                 System.out.println("whiteLines:" + whiteLines);
  25.                
  26.         }

  27.         private static void parse(File f) {
  28.                 BufferedReader br = null;
  29.                 boolean comment = false;
  30.                 try {
  31.                         br = new BufferedReader(new FileReader(f));
  32.                         String line = "";
  33.                         while((line = br.readLine()) != null) {
  34.                                 line = line.trim();
  35.                                 if(line.matches("^[\\s&&[^\\n]]*$")) {
  36.                                         whiteLines ++;
  37.                                 } else if (line.startsWith("/*") && !line.endsWith("*/")) {
  38.                                         commentLines ++;
  39.                                         comment = true;       
  40.                                 } else if (line.startsWith("/*") && line.endsWith("*/")) {
  41.                                         commentLines ++;
  42.                                 } else if (true == comment) {
  43.                                         commentLines ++;
  44.                                         if(line.endsWith("*/")) {
  45.                                                 comment = false;
  46.                                         }
  47.                                 } else if (line.startsWith("//")) {
  48.                                         commentLines ++;
  49.                                 } else {
  50.                                         normalLines ++;
  51.                                 }
  52.                         }
  53.                 } catch (FileNotFoundException e) {
  54.                         e.printStackTrace();
  55.                 } catch (IOException e) {
  56.                         e.printStackTrace();
  57.                 } finally {
  58.                         if(br != null) {
  59.                                 try {
  60.                                         br.close();
  61.                                         br = null;
  62.                                 } catch (IOException e) {
  63.                                         e.printStackTrace();
  64.                                 }
  65.                         }
  66.                 }
  67.         }

  68. }
复制代码

作者: 柯伟斌    时间: 2012-8-15 11:02
可以用哦~{:soso_e113:}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2