TestCodeRowCount_Demo

本文介绍了一个使用Java编写的代码统计器,该统计器能够解析文件并统计其中的空白行、代码行和注释行数量。通过正则表达式匹配和模式识别,此工具能准确地区分不同类型的行,为代码审查和维护提供数据支持。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class CodeCounter {
    static int whiteLine = 0;
    static int codeLine = 0;
    static int commentLine = 0;
    public static void main(String[] args) {
        parse();
        System.out.println(whiteLine);
        System.out.println(codeLine);
        System.out.println(commentLine);
    }

    private static void parse() {
        //Pattern p = Pattern.compile("[\\w-]+@[\\w[-.]]+\\.[\\w]+");
        Pattern p = Pattern.compile("^[\\s&&[^\\n]]*$");

        BufferedReader br = null;
        String line = null;
        boolean comment = false;
        try {
            br = new BufferedReader(new FileReader("F:\\download\\Google\\csdn.html"));

            while((line = br.readLine()) != null) {
                line.trim();
                Matcher m = p.matcher(line);
                if(m.find()){
                    whiteLine ++;
                } else if(line.startsWith("/*") && !line.endsWith("*/")) {
                    comment = true;
                    commentLine ++;
                } else if (true == comment) {
                    commentLine ++;
                    if(line.endsWith("*/")){
                        comment = false;
                    }
                } else if (line.startsWith("/*") && line.endsWith("*/")) {
                    commentLine ++;
                } else {
                    codeLine ++;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值