java - inputStream关闭了,还有必要关闭InputStreamReader和BufferedReader吗?

查看:1999
本文介绍了java - inputStream关闭了,还有必要关闭InputStreamReader和BufferedReader吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

InputStream is = new FileInputStream(path);
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);

如上代码,在使用完以后需要每个都关闭吗?还是只要关闭is就行了

解决方案

直接调用最外层的close()就可以了


update:

http://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html#close--

Closes the stream and releases any system resources associated with it

http://docs.oracle.com/javase/8/docs/api/java/io/InputStreamReader.html#close--

Closes the stream and releases any system resources associated with it

就是一个链条 你关闭最外面的 最外面的关闭里面的 里面的关闭更里面的


当然你如果硬要说关闭可能失败 一定要finally一下我也没办法

这篇关于java - inputStream关闭了,还有必要关闭InputStreamReader和BufferedReader吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆