• javaWeb代码工程统计


    直接放在src/test/java包内运行

    /**
     * 代码行数统计
     * @author ThinkGem
     * @version 2014-7-22
     */
    public class CodeCounter {
    
        public static void main(String[] args) {
    //获取当前类的资源文件路径,即../../target/test-classes/ String file
    = CodeCounter.class.getResource("/").getFile(); // System.out.println(file); //path=D:/Source_Code/koudaimini/src/
    //设置统计根路径为src
    // String path = file.replace("target/test-classes", "src"); String path = "D:\Source_Code\zbg\modules\zbg-api\src"; ArrayList<File> al = CodeCounter.getFile(new File(path)); for (File f : al) { if (f.getName().matches(".*\.java$")){ // 匹配java格式的文件 CodeCounter.count(f); System.out.println(f); } } System.out.println("统计文件:" + CodeCounter.files); System.out.println("代码行数:" + CodeCounter.codeLines); System.out.println("注释行数:" + CodeCounter.commentLines); System.out.println("空白行数:" + CodeCounter.blankLines); } static long files = 0; static long codeLines = 0; static long commentLines = 0; static long blankLines = 0; static ArrayList<File> fileArray = new ArrayList<File>(); /** * 获得目录下的文件和子目录下的文件 * @param f * @return */ public static ArrayList<File> getFile(File f) { File[] ff = f.listFiles(); for (File child : ff) { if (child.isDirectory()) { CodeCounter.getFile(child); } else { CodeCounter.fileArray.add(child); } } return CodeCounter.fileArray; } /** * 统计方法 * @param f */ private static void count(File f) { BufferedReader br = null; boolean flag = false; try { br = new BufferedReader(new FileReader(f)); String line = ""; while ((line = br.readLine()) != null) { line = line.trim(); // 除去注释前的空格 if (line.matches("^[ ]*$")) { // 匹配空行 CodeCounter.blankLines++; } else if (line.startsWith("//")) { CodeCounter.commentLines++; } else if (line.startsWith("/*") && !line.endsWith("*/")) { CodeCounter.commentLines++; flag = true; } else if (line.startsWith("/*") && line.endsWith("*/")) { CodeCounter.commentLines++; } else if (flag == true) { CodeCounter.commentLines++; if (line.endsWith("*/")) { flag = false; } } else { CodeCounter.codeLines++; } } CodeCounter.files++; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); br = null; } catch (IOException e) { e.printStackTrace(); } } } } }

    类似结果

    D:/。。/koudaimini/src/
    统计文件:46
    代码行数:2798
    注释行数:814
    空白行数:652
  • 相关阅读:
    你的程序员女孩「GitHub 热点速览 v.22.09」
    高手过招不用鼠标,一款超好用的跨平台命令行界面库
    程序员的情人节「GitHub 热点速览 v.22.07」
    火爆的文字游戏你玩了吗?「GitHub 热点速览 v.22.06」
    SpringBoot入门
    将Bean放入Spring容器中的五种方式
    retransformClasses代码增强摘要
    网路代理技术
    Linux版钉钉使用
    deepin 创建桌面图标
  • 原文地址:https://www.cnblogs.com/gne-hwz/p/10265369.html
Copyright © 2020-2023  润新知