無線&移動互聯網技術研發

          換位思考·····
          posts - 19, comments - 53, trackbacks - 0, articles - 283
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          計算源代碼行數

          Posted on 2009-06-27 22:16 Gavin.lee 閱讀(576) 評論(0)  編輯  收藏 所屬分類: java SE & EE
          今天心血來潮,想算算目前項目代碼行數,呵呵,不藏私,大家共享一下。
          package com.yixun.wap;

          import java.io.BufferedReader;
          import java.io.File;
          import java.io.FileReader;
          import java.util.ArrayList;
          import java.util.List;

          /**
           * **********************************************
           * @description 計算源代碼(src)行數,不計算空行
           *     宗旨:將src下所有文件組裝成list,再篩選出文件,對文件進行遍歷讀取
           * 
          @author Gavin.lee
           * @date Jun 27, 2009    2:00:12 PM
           * 
          @version 1.0
           ***********************************************
           
          */

          public class LineCounter {
              List
          <File> list = new ArrayList<File>();
              
          int linenumber = 0;
              
              FileReader fr 
          = null;
              BufferedReader br 
          = null;

              
          public void counter() {
          //        String path = System.getProperty("user.dir");
                  String path = LineCounter.class.getResource("/").getPath();  // 同下個path
                  path = path.substring(0, path.length() - 24+ "src";
                  System.out.println(path);
                  File file 
          = new File(path);
                  File files[] 
          = null;
                  files 
          = file.listFiles();
                  addFile(files);
                  isDirectory(files);
                  readLinePerFile();
                  System.out.println(
          "Totle:" + linenumber + "");
              }


              
          // 判斷是否是目錄
              public void isDirectory(File[] files) {
                  
          for (File s : files) {
                      
          if (s.isDirectory()) {
                          File file[] 
          = s.listFiles();
                          addFile(file);
                          isDirectory(file);
                          
          continue;
                      }

                  }

              }


              
          //將src下所有文件組織成list
              public void addFile(File file[]) {
                  
          for (int index = 0; index < file.length; index++{
                      list.add(file[index]);
                      
          // System.out.println(list.size());
                  }

              }

              
              
          //讀取非空白行
              public void readLinePerFile() {
                  
          try {
                      
          for (File s : list) {
                          
          int yuan = linenumber;
                          
          if (s.isDirectory()) {
                              
          continue;
                          }

                          fr 
          = new FileReader(s);
                          br 
          = new BufferedReader(fr);
                          String i 
          = "";
                          
          while ((i = br.readLine()) != null{
                              
          if (isBlankLine(i))
                                  linenumber
          ++;
                          }

                          System.out.print(s.getName());
                          System.out.println(
          "\t\t有" + (linenumber - yuan) + "");
                      }

                  }
           catch (Exception e) {
                      e.printStackTrace();
                  }
           finally {
                      
          if (br != null{
                          
          try {
                              br.close();
                          }
           catch (Exception e) {
                          }

                      }

                      
          if (fr != null{
                          
          try {
                              fr.close();
                          }
           catch (Exception e) {
                          }

                      }

                  }

              }


              
          //是否是空行
              public boolean isBlankLine(String i) {
                  
          if (i.trim().length() == 0{
                      
          return false;
                  }
           else {
                      
          return true;
                  }

              }

              
              
          public static void main(String args[]) {
                  LineCounter lc 
          = new LineCounter();
                  lc.counter();
              }

          }
          主站蜘蛛池模板: 桐城市| 哈尔滨市| 莫力| 唐河县| 大关县| 鹤峰县| 进贤县| 龙井市| 杭锦后旗| 民县| 大英县| 承德市| 澄迈县| 黔江区| 怀安县| 莱西市| 永川市| 怀集县| 萨嘎县| 秦安县| 东乌珠穆沁旗| 大兴区| 四平市| 肥城市| 黄山市| 无为县| 泰兴市| 行唐县| 白山市| 波密县| 横峰县| 双牌县| 广灵县| 江川县| 常宁市| 木兰县| 刚察县| 资溪县| 故城县| 揭西县| 普兰县|