紀念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 閱讀(3228) 評論(0)  編輯  收藏


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


          網站導航:
           

          導航

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

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 育儿| 桦南县| 利津县| 深水埗区| 江都市| 城市| 甘南县| 隆昌县| 柯坪县| 海安县| 南阳市| 建水县| 屯门区| 平潭县| 武义县| 巴彦淖尔市| 新民市| 抚顺市| 奉贤区| 绩溪县| 绍兴市| 宁城县| 阿荣旗| 科技| 怀来县| 西乌珠穆沁旗| 黑龙江省| 尉氏县| 赣州市| 泰来县| 贺兰县| 南江县| 万安县| 库车县| 岱山县| 金沙县| 平罗县| 衢州市| 资兴市| 乌恰县| 观塘区|