加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式--统计代码量

发布时间:2020-12-14 04:23:29 所属栏目:百科 来源:网络整理
导读:import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class MainClass { static long normalline = 0 ; static long commetline = 0 ; static long
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class MainClass {

    static long normalline = 0;
    static long commetline = 0;
    static long writeline = 0;

    public static void main(String[] args) {

        File file = new File("D:java_projectsMainTestClasssrc");
        File[] allfiles = file.listFiles();
        for (File child : allfiles) {
            if (child.getName().matches(".*.java$")) {
                parse(child);
            }
        }
        System.out.println("normalline="+normalline);
        System.out.println("commentline="+commetline);
        System.out.println("whiteline="+writeline);
    }
    /* zhu shi */
    /* * zhu shi */
    private static void parse(File child) {
        boolean comment=false;
        BufferedReader bufferedReader=null;
        try {
            bufferedReader = new BufferedReader(new FileReader(child));
            String line="";
            while ((line=bufferedReader.readLine())!=null) {
                line = line.trim();
                if (line.matches("^[s&&[^n]]*$")) {
                    writeline++;
                }else if (line.startsWith("/*") && !line.endsWith("*/")) {
                    commetline++;
                    comment=true;
                }else if (comment==true) {
                    commetline++;
                    if (line.endsWith("*/")) {
                        comment=false;
                    }
                }else if (line.startsWith("//")) {
                    commetline++;
                }else if (line.startsWith("/*")&&line.endsWith("*/")) {
                    commetline++;
                }else {
                    normalline++;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}

output:

normalline=54
commentline=4
whiteline=4

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读