• 丁又专老师作业——Java检测代码


    package com.util;
    
    import java.io.*;
    import java.util.regex.Pattern;
    
    /**
     * 代码统计工具 * 统计某个java源文件或某个目录中所有java源文件中注释、空白行、代码行各占多少行 * 注释:包括单行注释(//)、多行注释、文档注释 * 空白行:一行内没有任何内容的表示空白行 * 代码行:以分号“;”结束的一条语句,可以统计为一行有效代码
     */
    public class CodeCounter {
        // 记录注释行数
        static long annotationLine = 0;
        // 记录空白行数
        static long blankLine = 0;
        // 记录有效代码的行数
        static long codeLine = 0;
        // 代码总行数
        static long totalLine = 0;
        // 文件总数
        static long fileCount = 0;
    
        public static void main(String[] args) throws FileNotFoundException {
            File file = new File("/Users/YikaJ/Desktop/test.java");
            //根据io读取文件
            codeStat(file);
            System.out.println("----------统计结果---------");
            System.out.println("文件数量:" + fileCount + "个");
            System.out.println(file + "文件/目录总行数:" + totalLine);
            System.out.println("代码行数:" + codeLine);
            System.out.println("注释行数:" + annotationLine);
            System.out.println("空白行数:" + blankLine);
            long otherLine = totalLine - (codeLine + annotationLine + blankLine);
            System.out.println("其它行数:" + otherLine);
        }
    
        private static void codeStat(File file) throws FileNotFoundException {
            if (file == null || !file.exists()) throw new FileNotFoundException(file + ",文件不存在!");
            fileCount++;    // 文件数累加
            if (file.isDirectory()) {
                File[] files = file.listFiles(new FileFilter() {
                    @Override //继承覆盖父类
                    public boolean accept(File pathname) {
                        return pathname.getName().endsWith(".java") || pathname.isDirectory();
                    }
                });
                //c#的是  foreach(File target in files)
                for (File target : files) {
                    codeStat(target);
                }
            } else {
                BufferedReader bufr;
                try {
                // 将指定路径的文件与字符流绑定
                    bufr = new BufferedReader(new FileReader(file));
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(file + ",文件不存在!" + e);
                }
                // 定义匹配每一行的正则匹配器
                // 注释匹配器(匹配单行、多行、文档注释)
                Pattern annotationLinePattern = Pattern.compile("((//)|(/\*+)|((^\s)*\*)|((^\s)*\*+/))+", Pattern.MULTILINE + Pattern.DOTALL);
                // 空白行匹配器(匹配回车、tab键、空格)
                Pattern blankLinePattern = Pattern.compile("^\s*$");
                // 代码行匹配器(以分号结束为一行有效语句,包括import和package语句)
                Pattern codeLinePattern = Pattern.compile(".+;\s*(((//)|(/\*+)).*)*", Pattern.MULTILINE + Pattern.DOTALL);
                // 遍历文件中的每一行,并根据正则匹配的结果记录每一行匹配的结果
                String line;
                try {
                    while ((line = bufr.readLine()) != null) {
                        if (annotationLinePattern.matcher(line).find()) {
                            annotationLine++;
                        }
                        if (blankLinePattern.matcher(line).find()) {
                            blankLine++;
                        }
                        if (codeLinePattern.matcher(line).find()) {
                            codeLine++;
    
                        }
                        totalLine++;
                    }
                } catch (IOException e) {
                    throw new RuntimeException("读取文件失败!" + e);
                } finally {
                    try {
                        bufr.close();
                        // 关闭文件输入流并释放系统资源
                    } catch (IOException e) {
                        throw new RuntimeException("关闭文件输入流失败!");
                    }
                }
            }
        }
    }
    
    

    对于,如何通过正则检验字符串中的"//out.print(var)",能力不足,还未实现。

  • 相关阅读:
    Log4j2 配置
    Spring + SpringMVC配置
    Tomcat 动态数据库连接池
    MySQL数据库备份命令
    一条insert语句插入数据库
    tomcat 性能优化
    linux RPM manager
    mysql 多主
    ceph学习
    python常用程序算法
  • 原文地址:https://www.cnblogs.com/YikaJ/p/4366761.html
Copyright © 2020-2023  润新知