打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
统计java代码行数和jar包中*.class代码的行数
  1. 自己写了一个简单的小工具,统计一下指定项目路径下java行数和指定路径下jar包中.class 文件的代码行数。  
  2. 具体内容如下:  
  1. 1:统计指定目录下所有的*.java 文件的代码行数,文件为JavaTotal.java(可单独运行);  
  1. import java.io.BufferedReader;  
  2. import java.io.BufferedWriter;  
  3. import java.io.File;  
  4. import java.io.FileReader;  
  5. import java.io.FileWriter;  
  6. import java.io.IOException;  
  7. import java.io.PrintWriter;  
  8. import java.util.ArrayList;  
  9. import java.util.List;  
  10. /** 
  11.  * 统计指定项目的目录下的*.java代码行数 
  12.  * @author aflyun 
  13.  * @date 2016.02.16 
  14.  * @version 1.0 
  15.  */  
  16. public class JavaTotal {  
  17.   
  18.     //项目java文件所在目录  
  19.     public static String javaPath = "D:/KuYuPrograms/tclshop/src/";  
  20.     //统计代码行数  
  21.     public static Integer countCode = 0;  
  22.       
  23.     public static int runJavaTotal(){  
  24.         try {  
  25.             File filetxtPath = new File("D:/javaFileCount.txt");//输出要统计的文件信息  
  26.             PrintWriter pw = new PrintWriter(new FileWriter(filetxtPath));  
  27.               
  28.             List<File> list = total(javaPath);  
  29.             for (File file : list) {  
  30.                 String javaName = file.getAbsolutePath().replace("\\", "/");  
  31.                 if(javaName.endsWith(".java")){  
  32.                     pw.println(javaName);  
  33.                 }  
  34.             }  
  35.             pw.println("总共java文件数量 :" + list.size());  
  36.             pw.close();  
  37.             System.err.println("java文件数量:"+list.size());  
  38.             countJavaLine(list);  
  39.             System.err.println("java中总代码行数:" + countCode);  
  40.               
  41.         } catch (Exception e) {  
  42.             // TODO: handle exception  
  43.         }  
  44.           
  45.         return countCode;  
  46.     }  
  47.     /** 
  48.      * 获取所有的文件 
  49.      * @param path 获取文件的路径 
  50.      * @return 
  51.      */  
  52.     public static List<File> total(String path){  
  53.         List<File> fileList = null;  
  54.         try {  
  55.             fileList = new ArrayList<File>();  
  56.             File filePath = new File(path);  
  57.             File[] files = filePath.listFiles();//listFiles能够获取当前文件夹下的所有文件和文件夹  
  58.             for (File file : files) {  
  59.                 if(file.isFile() && file.getName().endsWith(".java")){  
  60.                     fileList.add(file);  
  61.                 }else {  
  62.                     fileList.addAll(fileList.size(), total(file.getPath()));  
  63.                 }  
  64.             }  
  65.         } catch (Exception e) {  
  66.             // TODO: handle exception  
  67.         }  
  68.           
  69.         return fileList;  
  70.     }  
  71.       
  72.     /** 
  73.      * 统计项目中java代码的行数 
  74.      * @param listFile 文件的集合 
  75.      */  
  76.     public static void countJavaLine(List<File> listFile){  
  77.         try {  
  78.             for (File file : listFile) {  
  79.                 if(file.getName().endsWith(".java")){  
  80.                       
  81.                     FileReader fr = new FileReader(file);  
  82.                     BufferedReader br = new BufferedReader(fr);  
  83.                     String line = "";  
  84.                     while((line = br.readLine()) != null){  
  85.                         countCode ++;  
  86.                     }  
  87.                 }  
  88.             }  
  89.         } catch (Exception e) {  
  90.             System.err.println("统计java代码行数出错!");  
  91.         }  
  92.           
  93.     }  
  94.       
  95. //==========================================================================================//  
  96.     public static void main(String[] args) throws IOException {  
  97.   
  98.         long start = System.nanoTime();  
  99.         runJavaTotal();  
  100.         long end = System.nanoTime();  
  101.         System.out.print("cost: " + (end - start)/1e9 + " seconds");  
  102.     }  
  103. }  
  104. </span>  

2:统计指定目录下所有的*.jar 包中*.class 文件的代码行数,文件为 JarTotal.Java(可单独运行)

  1. package com.dufy.test;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileWriter;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8. import java.io.InputStreamReader;  
  9. import java.io.PrintWriter;  
  10. import java.net.URL;  
  11. import java.util.Enumeration;  
  12. import java.util.HashSet;  
  13. import java.util.Set;  
  14. import java.util.jar.JarEntry;  
  15. import java.util.jar.JarFile;  
  16. /** 
  17.  * 统计指定路径下面jar包文件中所有*.class 文件的代码行数 
  18.  * @author aflyun 
  19.  * @date 2016.02.16 
  20.  * @version 1.0 
  21.  */  
  22. public class JarTotal {  
  23.   
  24.     //jar包存放的仓库位置  
  25.     public static String jarPath = "D:/KuYuPrograms/repository/com/aebiz";   
  26.     //存放所有的jar的包路径和名称  
  27.     public static Set<String> jarList = new HashSet<String>();  
  28.     //统计jar包总代码行数  
  29.     public static int countCode = 0;  
  30.       
  31.     public static int runJarTotal() {  
  32.         try {  
  33.             File filetxtPath = new File("D:/jarFileCount.txt");//输出要统计的文件信息  
  34.             PrintWriter pw = new PrintWriter(new FileWriter(filetxtPath));  
  35.             File file = new File(jarPath);  
  36.                 findAllJarFiles(file);  
  37.             for (String jarName : jarList) {  
  38.                 pw.println(jarName); //将jar文件写入txt中  
  39.                 Set<String> findAllJarClassfiles = findAllJarClassfiles(jarName);  
  40.                 for (String jarClassFileName : findAllJarClassfiles) {  
  41.                         countJarLine(jarName,jarClassFileName);  
  42.                 }  
  43.             }  
  44.             pw.println("总共jar文件数量 :" + jarList.size());  
  45.             pw.close();  
  46.             System.err.println("jar包文件数量 :  "+ jarList.size());  
  47.             System.err.println("jar包中总代码行数 :  "+ countCode);  
  48.               
  49.         } catch (Exception e) {  
  50.             // TODO: handle exception  
  51.         }  
  52.           
  53.         return countCode;  
  54.     }  
  55.       
  56.     /** 
  57.      * 遍历获取所有的jar包文件路径和名称 
  58.      * @param dir 目标路径 
  59.      */  
  60.      public static void findAllJarFiles(File dir) {  
  61.          try {  
  62.              //获取当前文件夹下的所有文件和文件夹  
  63.              File[] files = dir.listFiles();  
  64.              for(int i = 0; i < files.length; i++){  
  65.                  // System.out.println(fs[i].getAbsolutePath());  
  66.                   String jspPath = files[i].getAbsolutePath().replace("\\", "/");  
  67.                   if(jspPath.endsWith(".jar")){  
  68.                       //System.out.println(jspPath);  
  69.                       jarList.add(jspPath);  
  70.                   }  
  71.                   //如果是文件夹,递归  
  72.                   if(files[i].isDirectory()){  
  73.                       findAllJarFiles(files[i]);  
  74.                   }  
  75.                   
  76.              }  
  77.         } catch (Exception e) {  
  78.             System.err.println("获取所有的jar包路径和名称出错!");  
  79.         }  
  80.           
  81.      }  
  82.        
  83.     /** 
  84.      * 获取jar包目录下所有的class文件 
  85.      * @param jarName jar包的路径和名称 
  86.      * @return  返回对应jar包下所有.class 文件的集合 
  87.      */  
  88.      public static Set<String> findAllJarClassfiles(String jarName){  
  89.         //存放jar包下对应的文件路径和名称  
  90.         Set<String> jarFileList = new HashSet<String>();  
  91.          try {  
  92.             JarFile jarFile = new JarFile(jarName);  
  93.             Enumeration<JarEntry> entries = jarFile.entries();  
  94.             while(entries.hasMoreElements()){  
  95.                 JarEntry jarEntry = entries.nextElement();  
  96.                 String fileName = jarEntry.getName();  
  97.                 if(fileName.endsWith(".class")){  
  98.                     //System.out.println(fileName);  
  99.                     jarFileList.add(fileName);  
  100.                 }  
  101.             }  
  102.         } catch (IOException e) {  
  103.             System.err.println("获取jar包下的所有class出错!");  
  104.         }  
  105.          return jarFileList;  
  106.      }  
  107.        
  108.     /** 
  109.      * 构造URI/URL格式的文件路径<br/> 
  110.      * 统计所有jar包中所有class文件的代码行数 
  111.      * @param jarName   jar包的路径和名称 
  112.      * @param jarClassFileName  jar包下所有文件.class 文件的路径和名称 
  113.      * @throws  IOException 
  114.      */  
  115.      public static void countJarLine(String jarName,String jarClassFileName) {  
  116.         try {  
  117.             URL url = new URL("jar:file:/"+jarName+"!/"+jarClassFileName+"");   
  118.             //System.out.println(url);   
  119.             InputStream is=url.openStream();   
  120.             BufferedReader br=new BufferedReader(new InputStreamReader(is));  
  121.             String line = "";  
  122.             while((line = br.readLine())!=null){  
  123.                 countCode ++;  
  124.             }  
  125.         } catch (Exception e) {  
  126.             System.err.println("统计jar包总代码数出错!");  
  127.         }  
  128.     }  
  129.        
  130. //==========================================================================================//        
  131.     public static void main(String[] args) throws Exception {  
  132.         long start = System.nanoTime();  
  133.         runJarTotal();  
  134.         long end = System.nanoTime();  
  135.         System.out.print("cost: " + (end - start)/1e9 + " seconds");  
  136.     }  
  137. }  

3:调用1、2中的工具类,统计出 项目中指定路径下 *.java 和指定jar包中*.class 的总代码行数,文件为 CountTotalMain.java

  1. package com.dufy.test;  
  2.   
  3. /** 
  4.  * 统计项目中所有代码的行数<br/> 
  5.  *  1: .java文件中代码<br/> 
  6.  *  2: jar包中的文件代码 
  7.  * @author aflyun 
  8.  * 
  9.  */  
  10. public class CountTotalMain {  
  11.   
  12.     public static void main(String[] args) {  
  13.         long start = System.nanoTime();  
  14.           
  15.         int runJavaTotal = JavaTotal.runJavaTotal();  
  16.         int runJarTotal = JarTotal.runJarTotal();  
  17.         System.out.println("java总代码: " + runJavaTotal +"----jar总代码: " + runJarTotal);  
  18.         System.out.println("项目中总代码之和为 : " + (runJarTotal + runJavaTotal));  
  19.           
  20.         long end = System.nanoTime();  
  21.         System.out.println("cost: " + (end - start)/1e9 + " seconds");  
  22.     }  
  23. }  


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Java或者JAR包获取读取资源文件的路径的问题总结
springboot 打jar 包部署时 读取外部配置文件
Java的类初始化的详解
java加载机制整理
Java 命令行工具使用
Java代码的编译与反编译那些事儿
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服