java统计代码的行数
生活随笔
收集整理的這篇文章主要介紹了
java统计代码的行数
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
java統(tǒng)計(jì)代碼的行數(shù),區(qū)別空行、注釋行、代碼行、配置行
package com.lxh.config.utils;import java.io.*;/*** @ClassName: CountCodeLines* @Author: lxh* @Description: 計(jì)算代碼的行數(shù)* @Date: 2022/8/1 14:53*/ public class CountCodeLines {/** 空行 */private static long nullLines = 0;/** 注釋行 */private static long annoLines = 0;/** 代碼行 */private static long codeLines = 0;/** 配置文件行 */private static long configLines = 0;/** 總行 */private static long allLines = 0;public static void main(String[] args) {CountCodeLines ccl = new CountCodeLines();ccl.listFile("D:\\Java\\gitCode\\fire-command-platform-code");System.out.println("空行:" + nullLines);System.out.println("注釋行:" + annoLines);System.out.println("代碼行:" + codeLines);System.out.println("配置文件行:" + configLines);System.out.println("總行:" + allLines);}/*** 循環(huán)文件夾統(tǒng)計(jì)* @param filePath*/private void listFile(String filePath) {File f = new File(filePath);File[] childs = f.listFiles();for (int i = 0; i < childs.length; i++) {if (!childs[i].isDirectory()) {if (childs[i].getName().matches(".*\\.java$")||childs[i].getName().endsWith(".yml")|| childs[i].getName().endsWith(".xml") // || childs[i].getName().endsWith(".properties")) {System.out.println(childs[i].getName());sumCode(childs[i]);}}else {listFile(childs[i].getPath());}}}/*** 統(tǒng)計(jì)代碼行數(shù)* @param file*/private void sumCode(File file){BufferedReader br = null;try{br = new BufferedReader(new FileReader(file));String line = "";while ((line = br.readLine()) != null){allLines++;if(file.getName().endsWith(".yml")|| file.getName().endsWith(".xml")|| file.getName().endsWith(".properties")){//配置文件configLines++;}else {//java文件String trimStr = line.trim();if (trimStr.length() == 0){//空行nullLines++;}else if (trimStr.startsWith("//")|| trimStr.startsWith("/**")|| trimStr.startsWith("*")|| trimStr.startsWith("*/")|| trimStr.startsWith("/*")){annoLines++;}else {codeLines++;}}}}catch (Exception e){e.printStackTrace();}finally {try {br.close();} catch (IOException e) {e.printStackTrace();}}}}總結(jié)
以上是生活随笔為你收集整理的java统计代码的行数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 美国大学生数学建模竞赛O奖最高级别国家一
- 下一篇: 【Java面试题】这道分布式面试题一定要