代码统计——过滤注释空行

通常我们自己想看看自己写了多少行代码——虽然这样很冠冕堂皇,废话少说了,直接上代码:

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

/**
 * Created by Kuring on 2016/8/8.
 */
public class CountRows {
    private long rows = 0L;
    private long chars = 0L;
    private long fileNum = 0L;
    private String fileType = "java";

    public static void main(String[] args) throws IOException {
        String path = args[0];
        String fileType = args[1];
        CountRows countRows = new CountRows();
        countRows.setFileType(fileType);
        System.out.println("开始计算目录 " + path + " 下文件的总行数...");
        countRows.countRows(path);
    }

    public String getFileType() {
        return fileType;
    }

    public void setFileType(String fileType) {
        this.fileType = fileType;
    }

    public void countRows(String path) throws IOException {
        File file = new File(path);
        getAllFile(file);
        System.out.println("统计文件数:" + this.fileNum);
        System.out.println("总字符数:" + this.chars + "个");
        System.out.println("总行数为:" + this.rows + " 行");
    }

    private void getAllFile(File file) throws IOException {
        File[] files = file.listFiles();
        for (File tmp : files
                ) {
            String fileName = tmp.getName();
            if (tmp.isFile() && fileName.endsWith(this.fileType)) {
                long chars = tmp.length();
                //System.out.println("文件名称:" + fileName);
                //System.out.println("文件字符数:" + chars);
                BufferedReader bufferedReader = new BufferedReader(new FileReader(tmp));
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    //按照编码规范剔除空行和注释行
                    //if (!line.trim().equals("")) {
                    //    if(!line.trim().startsWith("/**") &&
                    //            !line.trim().startsWith("*") &&
                    //            !line.trim().startsWith("*/") &&
                    //            !line.trim().startsWith("//")) {
                    //        this.rows++;
                    //    }
                    //}
                    this.rows++;
                }
                this.chars += chars;
                this.fileNum++;
            } else if (tmp.isDirectory()) {
                getAllFile(tmp);
            }
        }
    }
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值