夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks
          <2012年12月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          公告

          本博客中未注原創的文章均為轉載,對轉載內容可能做了些修改和增加圖片注釋,如果侵犯了您的版權,或沒有注明原作者,請諒解

          常用鏈接

          留言簿(21)

          隨筆分類(644)

          隨筆檔案(669)

          文章檔案(6)

          最新隨筆

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          import java.io.BufferedInputStream;
          import java.io.FileInputStream;
          import java.io.FileReader;
          import java.io.InputStream;
          import java.io.LineNumberReader;
          import java.util.Calendar;

          public class TestRowCount {
              
          public static void main(String[] args) {
                  
          long datestart = Calendar.getInstance().getTimeInMillis();
                  
          int count = getFileLineCounts("XXX");
                  System.out.println(count);
                  
          long dateend = Calendar.getInstance().getTimeInMillis();
                  System.out.println((dateend 
          - datestart) / 1000);
              }


              
          public static int getFileLineCount(String filename) {
                  
          int cnt = 0;
                  LineNumberReader reader 
          = null;
                  
          try {
                      reader 
          = new LineNumberReader(new FileReader(filename));
                      @SuppressWarnings(
          "unused")
                      String lineRead 
          = "";
                      
          while ((lineRead = reader.readLine()) != null{
                      }

                      cnt 
          = reader.getLineNumber();
                  }
           catch (Exception ex) {
                      cnt 
          = -1;
                      ex.printStackTrace();
                  }
           finally {
                      
          try {
                          reader.close();
                      }
           catch (Exception ex) {
                          ex.printStackTrace();
                      }

                  }

                  
          return cnt;
              }


              
          public static int getFileLineCounts(String filename) {
                  
          int cnt = 0;
                  InputStream is 
          = null;
                  
          try {
                      is 
          = new BufferedInputStream(new FileInputStream(filename));
                      
          byte[] c = new byte[1024];
                      
          int readChars = 0;
                      
          while ((readChars = is.read(c)) != -1{
                          
          for (int i = 0; i < readChars; ++i) {
                              
          if (c[i] == '\n'{
                                  
          ++cnt;
                              }

                          }

                      }

                  }
           catch (Exception ex) {
                      cnt 
          = -1;
                      ex.printStackTrace();
                  }
           finally {
                      
          try {
                          is.close();
                      }
           catch (Exception ex) {
                          ex.printStackTrace();
                      }

                  }

                  
          return cnt;
              }

          }
          總結:
          每行的字符較多時, 使用第一個方法效率較高
          每行的字符較少時, 使用第二個方法效率較高
          posted on 2012-12-06 13:39 HUIKK 閱讀(2850) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 盐城市| 江安县| 西乌珠穆沁旗| 梅河口市| 林口县| 昔阳县| 昌乐县| 新余市| 金门县| 喜德县| 法库县| 新晃| 金秀| 太保市| 南皮县| 长子县| 房山区| 吉木乃县| 崇礼县| 佳木斯市| 韩城市| 仁怀市| 左权县| 龙州县| 登封市| 呼和浩特市| 平潭县| 和政县| 新邵县| 博野县| 乡城县| 奎屯市| 南汇区| 诸城市| 临高县| 华坪县| 密山市| 洪洞县| 巴马| 江川县| 海丰县|