本文介绍了我是否需要关闭()FileReader和BufferedReader?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用围绕FileReader的BufferedReader读取本地文件:

I'm reading a local file using a BufferedReader wrapped around a FileReader:

BufferedReader reader = new BufferedReader(new FileReader(fileName));
// read the file
// (error handling snipped)
reader.close();

我需要 close() FileReader ,或者包装器会处理吗?
我见过人们这样做的代码:

Do I need to close() the FileReader as well, or will the wrapper handle that?I've seen code where people do something like this:

FileReader fReader = new FileReader(fileName);
BufferedReader bReader = new BufferedReader(fReader);
// read the file
// (error handling snipped)
bReader.close();
fReader.close();

这个方法是从servlet调用的,我想确保不离开任何句柄都打开。

This method is called from a servlet, and I'd like to make sure I don't leave any handles open.

推荐答案

没有。

BufferedReader.close()

关闭流到javadoc for 和

closes the stream according to javadoc for BufferedReader and InputStreamReader

以及

FileReader.close()

确实。

这篇关于我是否需要关闭()FileReader和BufferedReader?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 02:51