日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

統(tǒng)計(jì)java代碼行數(shù)和jar包中*.class代碼的行數(shù)

 陳永正的圖書館 2017-04-10
  1. 自己寫了一個(gè)簡單的小工具,統(tǒng)計(jì)一下指定項(xiàng)目路徑下java行數(shù)和指定路徑下jar包中.class 文件的代碼行數(shù)。  
  2. 具體內(nèi)容如下:  
  1. 1:統(tǒng)計(jì)指定目錄下所有的*.java 文件的代碼行數(shù),文件為JavaTotal.java(可單獨(dú)運(yùn)行);  


  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.  * 統(tǒng)計(jì)指定項(xiàng)目的目錄下的*.java代碼行數(shù) 
  12.  * @author aflyun 
  13.  * @date 2016.02.16 
  14.  * @version 1.0 
  15.  */  
  16. public class JavaTotal {  
  17.   
  18.     //項(xiàng)目java文件所在目錄  
  19.     public static String javaPath = "D:/KuYuPrograms/tclshop/src/";  
  20.     //統(tǒng)計(jì)代碼行數(shù)  
  21.     public static Integer countCode = 0;  
  22.       
  23.     public static int runJavaTotal(){  
  24.         try {  
  25.             File filetxtPath = new File("D:/javaFileCount.txt");//輸出要統(tǒng)計(jì)的文件信息  
  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文件數(shù)量 :" + list.size());  
  36.             pw.close();  
  37.             System.err.println("java文件數(shù)量:"+list.size());  
  38.             countJavaLine(list);  
  39.             System.err.println("java中總代碼行數(shù):" + 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能夠獲取當(dāng)前文件夾下的所有文件和文件夾  
  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.      * 統(tǒng)計(jì)項(xiàng)目中java代碼的行數(shù) 
  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("統(tǒng)計(jì)java代碼行數(shù)出錯(cuò)!");  
  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:統(tǒng)計(jì)指定目錄下所有的*.jar 包中*.class 文件的代碼行數(shù),文件為 JarTotal.Java(可單獨(dú)運(yùn)行)

  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.  * 統(tǒng)計(jì)指定路徑下面jar包文件中所有*.class 文件的代碼行數(shù) 
  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.     //統(tǒng)計(jì)jar包總代碼行數(shù)  
  29.     public static int countCode = 0;  
  30.       
  31.     public static int runJarTotal() {  
  32.         try {  
  33.             File filetxtPath = new File("D:/jarFileCount.txt");//輸出要統(tǒng)計(jì)的文件信息  
  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文件數(shù)量 :" + jarList.size());  
  45.             pw.close();  
  46.             System.err.println("jar包文件數(shù)量 :  "+ jarList.size());  
  47.             System.err.println("jar包中總代碼行數(shù) :  "+ countCode);  
  48.               
  49.         } catch (Exception e) {  
  50.             // TODO: handle exception  
  51.         }  
  52.           
  53.         return countCode;  
  54.     }  
  55.       
  56.     /** 
  57.      * 遍歷獲取所有的jar包文件路徑和名稱 
  58.      * @param dir 目標(biāo)路徑 
  59.      */  
  60.      public static void findAllJarFiles(File dir) {  
  61.          try {  
  62.              //獲取當(dāng)前文件夾下的所有文件和文件夾  
  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包路徑和名稱出錯(cuò)!");  
  79.         }  
  80.           
  81.      }  
  82.        
  83.     /** 
  84.      * 獲取jar包目錄下所有的class文件 
  85.      * @param jarName jar包的路徑和名稱 
  86.      * @return  返回對應(yīng)jar包下所有.class 文件的集合 
  87.      */  
  88.      public static Set<String> findAllJarClassfiles(String jarName){  
  89.         //存放jar包下對應(yīng)的文件路徑和名稱  
  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出錯(cuò)!");  
  104.         }  
  105.          return jarFileList;  
  106.      }  
  107.        
  108.     /** 
  109.      * 構(gòu)造URI/URL格式的文件路徑<br/> 
  110.      * 統(tǒng)計(jì)所有jar包中所有class文件的代碼行數(shù) 
  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("統(tǒng)計(jì)jar包總代碼數(shù)出錯(cuò)!");  
  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:調(diào)用1、2中的工具類,統(tǒng)計(jì)出 項(xiàng)目中指定路徑下 *.java 和指定jar包中*.class 的總代碼行數(shù),文件為 CountTotalMain.java

  1. package com.dufy.test;  
  2.   
  3. /** 
  4.  * 統(tǒng)計(jì)項(xiàng)目中所有代碼的行數(shù)<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("項(xiàng)目中總代碼之和為 : " + (runJarTotal + runJavaTotal));  
  19.           
  20.         long end = System.nanoTime();  
  21.         System.out.println("cost: " + (end - start)/1e9 + " seconds");  
  22.     }  
  23. }  


    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多