统计java代码行数

package com.nsj.tool.util;

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

public class CodeCounter {

 static long normalLines = 0;

 static long commentLines = 0;

 static long blankLines = 0;

 public static void main(String[] args) {
  File f = new File("D://D//JavaWork//http//httpclientdemo//src");
  getFiles(f);
  System.out.println("SUM CODELINE:" + normalLines);
  System.out.println("SUM COMMENTLINE:" + commentLines);
  System.out.println("SUM BLANKLINE:" + blankLines);
 }

 private static void getFiles(File f) {
  File[] codeFiles = f.listFiles();
  for (int i = 0; i < codeFiles.length; i++) {
   if (codeFiles[i].isDirectory()) {
    getFiles(codeFiles[i]);
   } else if (codeFiles[i].getName().matches(".*//.java$")) {
    System.out.println(codeFiles[i].getName());
    parse_java(codeFiles[i]);
   }
  }
 }

 private static void parse_java(File f) {
  long normalLines1 = 0;
  long commentLines1 = 0;
  long blankLines1 = 0;

  BufferedReader br = null;
  boolean comment = false;
  try {
   br = new BufferedReader(new FileReader(f));
   String line = "";
   while ((line = br.readLine()) != null) {
    line = line.trim();
    //line = line.replaceAll(" ", "");
    if (line.matches("^[[//s]&&[^//n]]*$")) { // spaceLine ?*$
     blankLines1++;
    } else if (line.startsWith("//")
      || (line.startsWith("/*") && line.endsWith("*/"))) {
     commentLines1++;
    } else if (line.startsWith("/*") && !line.endsWith("*/")) {
     commentLines1++;
     comment = true;
    } else if ((line.endsWith("*/")) && comment == true) {
     commentLines1++;
     comment = false;
    } else if (line.endsWith("*/")) {
     commentLines1++;
     comment = false;
    } else if (comment == true) {
     commentLines1++;
    } else {
     normalLines1++;
    }
   }
   System.out.println(f.getName() + " CODELINE: " + normalLines1);
   System.out.println(f.getName() + " COMMENTLINE: " + commentLines1);
   System.out.println(f.getName() + " BLANKLINE: " + blankLines1);

   normalLines += normalLines1;
   commentLines += commentLines1;
   blankLines += blankLines1;
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   if (br != null) {
    try {
     br.close();
     br = null;
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 }
 

 private static void parse_vb(File f) {
  long normalLines1 = 0;
  long commentLines1 = 0;
  long blankLines1 = 0;

  BufferedReader br = null;
  try {
   br = new BufferedReader(new FileReader(f));
   String line = "";
   while ((line = br.readLine()) != null) {
    line = line.trim();
    //line = line.replaceAll(" ", "");
    if (line.matches("^[[//s]&&[^//n]]*$")) { // spaceLine ?*$
     blankLines1++;
    } else if (line.startsWith("'")) {
     commentLines1++;
    } else {
     normalLines1++;
    }
   }
   System.out.println(f.getName() + " CODELINE: " + normalLines1);
   System.out.println(f.getName() + " COMMENTLINE: " + commentLines1);
   System.out.println(f.getName() + " BLANKLINE: " + blankLines1);

   normalLines += normalLines1;
   commentLines += commentLines1;
   blankLines += blankLines1;
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } finally {
   if (br != null) {
    try {
     br.close();
     br = null;
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值