java关闭io流_为什么要关闭和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 阅读(3050) 评论(0)  编辑  收藏

你可能感兴趣的:(java关闭io流)