统计代码行数

     项目做到现在突然想从一个侧面来反映自己的劳动成果——那就是代码行数。 在网上找统计代码行数的工具,找了好几个不但不可以用而且还携带了病毒... ... 于是自己动手写了一个, 是单个程序版的,有待日后完善... ...  ^_^  ^_^  

    package schoolFellow;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class JavaCodeCount {

 private static int normallines = 0;
 private static int commentlines = 0;
 private static int spacelines = 0;
 
 public static void main(String args[]) {
  JavaCodeCount test = new JavaCodeCount();
  File file = new File("D://MyEclipse2//My_MVC//Dog//src");// 引号中输入路径
  test.list(file);
  
  System.out.println("normallines: " + normallines);
  System.out.println("commentlines: " + commentlines);
  System.out.println("spacelines: " + spacelines);
 }
 
 public void list(File file) {
  if(file.isDirectory()) {
   File[] childs = file.listFiles();
   for(File temp:childs) {
    list(temp);
   }
  } else if(file.getName().matches(".+java$")) {
   count(file);
  }
 }

 private void count(File file) {
  BufferedReader br = null;
  try {
   br = new BufferedReader(new FileReader(file));
   String str;
   boolean flag = false;
   while((str = br.readLine()) != null) {
    str = str.trim();
    if(str.matches("//s*$")) {
     spacelines ++;
    } else if(str.startsWith("//")) {
     commentlines ++;
    } else if(str.startsWith("/*") && str.endsWith("*/")) {
     commentlines ++;
    } else if(str.startsWith("/*")) {
     commentlines ++;
     flag = true;
    } else if(flag == true) {
     commentlines ++;
     if(str.endsWith("*/")) {
      flag = false;
     }
    } else {
     normallines ++;
    }
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   if(br != null) {
    try {
     br.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值