夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks
          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 閱讀(2839) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 德令哈市| 镇宁| 浙江省| 三都| 伊吾县| 乐至县| 丰顺县| 策勒县| 买车| 苗栗市| 汤原县| 台东市| 咸丰县| 延安市| 青铜峡市| 珲春市| 信阳市| 玛纳斯县| 阳东县| 永春县| 准格尔旗| 抚顺市| 黑山县| 隆昌县| 连云港市| 板桥市| 名山县| 鲜城| 谷城县| 金塔县| 襄城县| 酒泉市| 穆棱市| 荥阳市| 策勒县| 涞源县| 浦江县| 屏南县| 淳安县| 济宁市| 嘉禾县|