原则:最好在任何时候使用InputStream或者OutputStream的时候,在finally中调用close()方法,显式关闭。

一个典型的示例

InputStream in = null;
try {
in = acquireStream();
...
} finally {
if (in != null) in.close();
}

fianlly中的if (in != null) in.close();也可以用IOUtils.closeQuietly(in);代替,需要Apache Commons-IO

为什么需要调用

InputStream的作用是用来表示从不同数据源产生输入的类,这些数据源包括:

  • 字节数组
  • String对象
  • 文件
  • 管道
  • 一个由其他种类的流组成的序列,以便我们可以将他们收集合并到一个流内。
  • 其他数据源,如Internet连接等

通常不使用close会导致内存泄露,垃圾回收机制会回收,但是最好自己显式关闭,这并不是特别关键。

关键是当InputStream的数据源是文件或者Internet连接的时候。

OutputStream的作用是如FileOutStream,当不调用close的时候,不会将缓存刷入文件中。

InputStream的数据源是文件时

一个InputStream会韩勇一个极小的kernel资源,一个低级的file handle。

当打开文件时,将文件读入到InputStream,会对文件加锁。当你不考虑文件加锁。当你需要读取另一个文件的时候,会打开一个新的InputStream,kernel会分配另一个descriptor(文件流),一直累加,而一个进程的文件描述表是优先,到最后文件描述表将用被用完,所以为了以防万一,最好每次使用完之后都加一个close()

测试代码

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; public class TestInputStream {
public static void main(String[] args) {
// createTestFile();
// testInputStreamWithOutClose();
// testInputStreamWithOneFileWithOutClose();
testInputStreamWithClose();
} /**
* 创建一万个测试文件
*/
public static void createTestFile() {
try {
for (int i = 0; i < 10000; i++) {
FileOutputStream fos = new FileOutputStream(new File(
"/Users/shenpengyan/Documents/workspace/Test/testInputStream/Test" + i));
fos.write(i);
fos.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /**
* 用不同文件,不使用close,有"Too many open files in system"报错
*/
public static void testInputStreamWithOutClose(){
try {
for (int i = 0; i < 10000; i++) {
FileInputStream fis = new FileInputStream(new File(
"/Users/shenpengyan/Documents/workspace/Test/testInputStream/Test" + i));
System.out.println(fis.toString() + " " + i);
// fis.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /**
* 用同一个文件,不加close,有"Too many open files in system"报错
*/
public static void testInputStreamWithOneFileWithOutClose(){
try {
for (int i = 0; i < 10000; i++) {
FileInputStream fis = new FileInputStream(new File(
"/Users/shenpengyan/Documents/workspace/Test/testInputStream/Test1"));
System.out.println(fis.toString() + " " + i);
// fis.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /**
* 加close,无"Too many open files in system"报错
*/
public static void testInputStreamWithClose(){
try {
for (int i = 0; i < 100000; i++) {
FileInputStream fis = new FileInputStream(new File(
"/Users/shenpengyan/Documents/workspace/Test/testInputStream/Test1"));
System.out.println(fis.toString() + " " + i);
fis.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }

当不使用close()方法时,会有报错:Too many open files in system

java.io.FileInputStream@7e349a0e 6079
java.io.FileNotFoundException: /Users/shenpengyan/Documents/workspace/Test/testInputStream/Test1 (Too many open files in system)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at TestInputStream.testInputStreamWithOneFileWithOutClose(TestInputStream.java:53)
at TestInputStream.main(TestInputStream.java:9)
InputStream的数据源是Internet连接时

这是我实际遇到的情况,实际业务情况为:我需要调用一个图片存储位置,用key读到的图片存到InputStream里面来进行进一步处理,而我忘记了对它进行close,经过测试,处理300左右的请求之后,就不能继续请求了,这是为什么呢?是因为InputStream没有被垃圾回收掉,还一直占用着连接,而图片服务商有连接数限制,导致之后的请求没有返回,被调用的InputStream类如下:

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException; import org.apache.http.client.methods.CloseableHttpResponse; public class ObjectInputStream extends FilterInputStream { private CloseableHttpResponse httpResponse; public ObjectInputStream(InputStream in, CloseableHttpResponse httpResponse) {
super(in);
this.httpResponse = httpResponse;
} public void close() throws IOException {
this.httpResponse.close();
try {
super.close();
} catch (SocketException e) {
// expected from some implementations because the stream is closed
}
}
}

当没有调用close方法的时候,没有调用httpResponse.close();,连接不会关掉,所以就尴尬了。

理论依据参考:http://stackoverflow.com/questions/26541513/why-is-it-good-to-close-an-inputstream

Java:浅谈InputStream的close方法的更多相关文章

  1. 浅谈 js 字符串 trim 方法之正则篇

    原文:浅谈 js 字符串 trim 方法之正则篇 关于 trim 其实没啥好说的,无非就是去除首位空格,对于现代浏览器来说只是简单的正则 /^\s+|\s+$/ 就可以搞定了.而且支持中文空格   等 ...

  2. 浅谈 js 字符串 search 方法

    原文:浅谈 js 字符串 search 方法 这是一个很久以前的事情了,好像是安心兄弟在学习js的时候做的练习.具体记不清了,今天就来简单分析下 search 究竟是什么用的. 从字面意思理解,一个是 ...

  3. 浅谈 js 对象 toJSON 方法

    前些天在<浅谈 JSON.stringify 方法>说了他的正确使用姿势,今天来说下 toJSON 方法吧.其实我觉得这货跟 toString 一个道理,他是给 stringify 方法字 ...

  4. JAVA IO流 InputStream流 Read方法

    read()首先我们来看这个没有参数的read方法,从(来源)输入流中(读取的内容)读取数据的下一个字节到(去处)java程序内部中,返回值为0到255的int类型的值,返回值为字符的ACSII值(如 ...

  5. [iOS、Unity、Android] 浅谈闭包的使用方法

    前言 我们经常所编程语言的的进步速度是落后于硬件的发展速度的. 但是最近几年,闭包语法在各个语言中都有自己的体现形式,例如 • C语言中使用函数指针作为回调函数的入口: • Java和C#语言中的La ...

  6. java 浅谈web系统当中的cookie和session会话机制

    一 Cookie: 1. Cookie翻译为小甜饼,有一种特殊的味道.cookie主要用来在(浏览器)客户端做记号用的.Cookie不属于java,Cookie是一种通用的机制,属于HTTP协议的一部 ...

  7. 浅谈 String 的 hashCode() 方法

    Java 中 hash 值的含义 hash 值主要是用来在散列存储结构中确定对象的存储地址的,提高对象的查询效率,如HashMap.HashTable等: 如果两个对象相同,那么这两个对象的 hash ...

  8. [Android&amp;Java]浅谈设计模式-代码篇:观察者模式Observer

    观察者,就如同一个人,对非常多东西都感兴趣,就好像音乐.电子产品.Game.股票等,这些东西的变化都能引起爱好者们的注意并时刻关注他们.在代码中.我们也有这种一种方式来设计一些好玩的思想来.今天就写个 ...

  9. 浅谈js的sort()方法

    如果调用该方法时没有使用参数,将按字母顺序对数组中的元素进行排序,说得更精确点,是按照字符编码(字符串Unicode码点)的顺序进行排序.要实现这一点,首先应把数组的元素都转换成字符串(如有必要),以 ...

随机推荐

  1. 【转】JAVA之动态代理

    转自:像少年啦飞驰 代理设计模式 定义:为其他对象提供一种代理以控制对这个对象的访问. 代理模式的结构如下图所示. 动态代理使用 java动态代理机制以巧妙的方式实现了代理模式的设计理念. 代理模式示 ...

  2. I.MX6 Ubuntu core porting

    /*********************************************************************** * I.MX6 Ubuntu core porting ...

  3. acdream 1408 "Money, Money, Money" (水)

    题意:给出一个自然数x,要求找两个自然数a和b,任意多个a和b的组合都不能等于x,且要可以组合成大于x的任何自然数.如果找不到就输出两个0.(输出任意一个满足条件的答案) 思路:x=偶数时,无法构成, ...

  4. Java [Leetcode 203]Remove Linked List Elements

    题目描述: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...

  5. java 中的Exception RuntimeException 区别

    在java的异常类体系中: 1.Error和RuntimeException是非检查型异常,其他的都是检查型异常; 2.所有方法都可以在不声明throws的情况下抛出RuntimeException及 ...

  6. HDU 5876 Sparse Graph

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  7. Map/Reduce之间的Partitioner接口

    一.Partitioner介绍 Partitioner的作用是对Mapper产生的中间结果进行分片,以便将同一分组的数据交给同一个Reduce处理,它直接影响Reduce阶段的负载均衡(个人理解:就是 ...

  8. Think Python Glossary

    一.The way of the program problem solving: The process of formulating a problem, finding a solution, a ...

  9. Number Sequence(KMP,判断子串 模板)

    题意: 给两数组,求一个是否是另一个的子数组,若是返回匹配的首位置 分析: KMP 入门 //扫描字符串A,并更新可以匹配到B的什么位置. #include <map> #include ...

  10. CAKeyframeAnimation

    之所以叫做关键帧动画是因为,这个类可以实现,某一属性按照一串的数值进行动画,就好像制作动画的时候一帧一帧的制作一样. 一般使用的时候  首先通过 animationWithKeyPath 方法 创建一 ...