java 正则表达式2 通过正则表达式统计代码行数

import java.io.*;
import java.util.regex.*;
import java.util.Scanner;
public class CodeCounter{
	public static int whiteCount = 0;			//空白行
	public static int normalCount = 0;			//代码行
	public static int commentCount = 0;			//注释行
	public static void main(String []args){
			String filePath = "";
			String endString = "";
			Scanner scanner= new Scanner(System.in);
			System.out.println("请输入您要统计的文件目录,或者文件名,分隔符为/");
			filePath = scanner.nextLine();
			File file = new File(filePath);

			System.out.println("请输入一个您要统计的以什么为结尾的文件如: java");
			endString = scanner.nextLine();
			
			if(null == file){
				System.out.println("打开文件失败,请查看路径是否正确");
				return ;
			}
			
			if(file.isFile()){				//如果只是一个标准文件
				if(file.getName().matches(".*\\."+endString+"$")){
					parse(file);
				}
			}
			else if(file.isDirectory()){//如果是一个目录
					File[] childs = file.listFiles();
					getCount(childs,endString);
					
			}

			/*if(file.isFile()){				//如果只是一个标准文件
				if(file.getName().matches(".*\\."+endString+"$")){
					parse(file);
				}
			}
			else if(file.isDirectory()){//如果是一个目录
					File[] childs = file.listFiles();
					System.out.println(childs.length);
					for(File child :childs){
						if(child.getName().matches(".*\\."+endString+"$")){
							parse(child);
						}
					}
					
			}*/
			System.out.println("空白行:"+whiteCount);
			System.out.println("代码行:"+normalCount);
			System.out.println("注释行:"+commentCount);
	}
	public static void getCount(File[]files,String endString){
		for(File file :files){
				if(file.isFile()){				//如果只是一个标准文件
					if(file.getName().matches(".*\\."+endString+"$")){
						parse(file);
					}
				}
				else if(file.isDirectory()){//如果是一个目录
						File[] childs = file.listFiles();
						getCount(childs,endString);
						
				}
		}	
	}
	private static void parse(File f){
			BufferedReader br  = null;
			String line = "";
			boolean comment = false; 						//用来标识注释的开始于结束
			try{
					br = new BufferedReader(new FileReader(f));
					while((line = br.readLine())!=null){			//该方法会去除结尾的\n
						line = line.trim();
						if(line.matches("^[\\t&&[^\\n]]*$")){
							whiteCount++;
						}else if(line.startsWith("/*")&&!line.endsWith("*/")){
								commentCount++;
								comment = true;
						}else if (true == comment){
								commentCount++;
								if(line.endsWith("*/")){
									comment = false;
								}
						}
						else if(line.startsWith("//")){
							commentCount++;
						}
						else{
							normalCount++;
						}


					}
			}catch (Exception e){
				e.printStackTrace();
			}finally{
				try{
					br.close();
					br = null;
				}catch(Exception e){
					e.printStackTrace();
				}
			}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值