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

前端之家收集整理的这篇文章主要介绍了正则表达式--统计代码量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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_projects\\MainTestClass\\src");
        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
原文链接:https://www.f2er.com/regex/359190.html

猜你在找的正则表达式相关文章