紀念SUN

          Powered By Andy

          為什么要關閉和io流

          package cn.webmctv.test;

          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileNotFoundException;
          import java.io.IOException;
          import java.io.InputStream;

          public class TestInputStream {

              public static void main(String[] args) {
                  /* 為什么要關閉和io流, 由于java底層是用c實現的, 所以當我們不停的調用new InputStream -> impl
                   * 時候, c打開的文件會一直沒有關閉,而導致文件刪除不了,別的程序訪問不了的問題,和操作系統打開文件
                   * 超過最大數異常。而下面new FileInputStream(new File("c:/q.txt"));這種方式沒有關閉c打開的
                   * 文件一直new 就會出現打開文件太多異常。
                  short count = 0;
                  InputStream inStream = null;
                      try {
                          for (int i = 0; i < Short.MAX_VALUE; i++) {
                              //inStream.
                              inStream = new FileInputStream(new File("/root/install.log"));
                              //count ++;
                              System.out.println("count: " + count++);
                          }
                          //p.load(inStream);
                      } catch (FileNotFoundException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      } finally{
                          if(inStream != null) try{ inStream.close(); } catch(IOException e){};
                      }
                  
                  System.out.println(Short.MAX_VALUE);
                  */
                  short count = 0;
                  InputStream inStream = null;
                  for (int i = 0; i < Short.MAX_VALUE; i++) {
                      try {
                              //inStream.
                              inStream = new FileInputStream(new File("/root/install.log"));
                              //count ++;
                              System.out.println("count: " + count++);
                          
                          //p.load(inStream);
                      } catch (FileNotFoundException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      } finally{
                          if(inStream != null) try{ inStream.close(); } catch(IOException e){};
                      }
                  }
                  
                  System.out.println(Short.MAX_VALUE);
                  
              }
          }

          posted on 2011-11-18 10:09 Powered By Andy 閱讀(3225) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          <2011年11月>
          303112345
          6789101112
          13141516171819
          20212223242526
          27282930123
          45678910

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 金沙县| 黄平县| 赤城县| 焉耆| 宁津县| 罗江县| 庆城县| 阳高县| 繁峙县| 陵川县| 察雅县| 自贡市| 威远县| 固镇县| 乌拉特中旗| 将乐县| 阿巴嘎旗| 通江县| 榆社县| 莲花县| 托克托县| 达孜县| 西丰县| 安阳县| 红桥区| 兴和县| 安福县| 南岸区| 仲巴县| 策勒县| 时尚| 广西| 香港| 乐至县| 施秉县| 西华县| 汝南县| 民县| 满洲里市| 兴安盟| 隆子县|