java实现项目代码行数的简单计算

package com.burning.BurningUtil.util;

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

public class CodeLineCounter {
	private static int totalLine;
	private static String fileToBeCounted[] = { "java", "jsp", "css",
			"html", "htm","xml"};//所需计算的源码文件类型
	private static int fileToBeCountedN = 6;//所需计算的源码文件类型个数

	private static boolean isFileToBeCounted(String type) {
		for (int i = 0; i < fileToBeCountedN; i++) {
			if (type.equals(fileToBeCounted[i]))
				return true;
		}
		return false;
	}

	private static String getType(String filename) {
		//System.out.println(filename);
		byte b[] = filename.getBytes();
		byte[] type = new byte[10];
		String rts = null;
		int i = 0, p = 0, n = filename.length();
		for (i = 0; i < n; i++) {
			if (b[i] == '.') {
				p = i;
				break;
			}
		}
		//System.out.print(p);
		i = p + 1;
		p = 0;
		for (; i < n&&p<10; i++) {
			type[p++] = b[i];
			//System.out.print(b[i]);
		}
		rts = new String(type);
		//System.out.println(rts.substring(0, p));
		return rts.substring(0, p);
	}

	private static void countLine(String path) {
		File file = new File(path);
		File lists[] = file.listFiles();
		for (int i = 0; i < lists.length; i++) {
			if (lists[i].isFile()) {
				String filename = lists[i].getName();
				boolean isFileToBeCounted = isFileToBeCounted(getType(filename));
				if (isFileToBeCounted) {
					try {
						int lines=0;
						FileReader read = new FileReader(path + filename);
						BufferedReader br = new BufferedReader(read);
						String row;
						while ((row = br.readLine()) != null) {
							lines++;
						}
						System.out.println("文件:" + path + filename+"共:"+lines+"行代码;");
						totalLine+=lines;
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			} else {
				String paths = path;
				paths = paths + lists[i].getName() + "\\";
				System.out.println("进入目录:" + paths+";");
				countLine(paths);
			}
		}
	}

	public static void main(String[] args) {
		totalLine = 0;
		String path = new String(
				"E:\\j2ee\\SSH\\EasyCMS\\");//项目的绝对地址
		//E:\\j2ee\\amigo\\amigo\\
		countLine(path);
		System.out.println("整个项目共:"+totalLine+"行代码;");
	}

}

 

 

 运行效果:
文件:E:\java\SSH\EasyCMS\WebRoot\WEB-INF\classes\applicationContext.xml共:84行代码;
进入目录:E:\java\SSH\EasyCMS\WebRoot\WEB-INF\classes\com\;
进入目录:E:\java\SSH\EasyCMS\WebRoot\WEB-INF\classes\com\burning\;
进入目录:E:\java\SSH\EasyCMS\WebRoot\WEB-INF\classes\com\burning\EasyCMS\;
进入目录:E:\java\SSH\EasyCMS\WebRoot\WEB-INF\classes\com\burning\EasyCMS\action\;
进入目录:E:\java\SSH\EasyCMS\WebRoot\WEB-INF\classes\com\burning\EasyCMS\dao\;
进入目录:E:\java\SSH\EasyCMS\WebRoot\WEB-INF\classes\com\burning\EasyCMS\dto\;
进入目录:E:\java\SSH\EasyCMS\WebRoot\WEB-INF\classes\com\burning\EasyCMS\entity\;
进入目录:E:\java\SSH\EasyCMS\WebRoot\WEB-INF\classes\com\burning\EasyCMS\myCkeditor\;
进入目录:E:\java\SSH\EasyCMS\WebRoot\WEB-INF\classes\com\burning\EasyCMS\service\;
进入目录:E:\java\SSH\EasyCMS\WebRoot\WEB-INF\classes\com\burning\EasyCMS\test\;
进入目录:E:\java\SSH\EasyCMS\WebRoot\WEB-INF\classes\com\burning\EasyCMS\util\;
文件:E:\java\SSH\EasyCMS\WebRoot\WEB-INF\classes\struts.xml共:421行代码;
文件:E:\java\SSH\EasyCMS\WebRoot\WEB-INF\index.jsp共:16行代码;
进入目录:E:\java\SSH\EasyCMS\WebRoot\WEB-INF\lib\;
文件:E:\java\SSH\EasyCMS\WebRoot\WEB-INF\web.xml共:35行代码;
整个项目共:14392行代码;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值