Java 我需要 close() FileReader 和 BufferedReader 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1388602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do I need to close() both FileReader and BufferedReader?
提问by Zilk
I'm reading a local file using a BufferedReader wrapped around a FileReader:
我正在使用环绕 FileReader 的 BufferedReader 读取本地文件:
BufferedReader reader = new BufferedReader(new FileReader(fileName));
// read the file
// (error handling snipped)
reader.close();
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:
我需要close()
的FileReader
为好,或将包装处理这个问题?我见过人们做这样的事情的代码:
FileReader fReader = new FileReader(fileName);
BufferedReader bReader = new BufferedReader(fReader);
// read the file
// (error handling snipped)
bReader.close();
fReader.close();
This method is called from a servlet, and I'd like to make sure I don't leave any handles open.
这个方法是从 servlet 调用的,我想确保我没有打开任何句柄。
采纳答案by Atmocreations
no.
不。
BufferedReader.close()
closes the streamaccording to javadoc for BufferedReaderand InputStreamReader
根据BufferedReader和InputStreamReader 的javadoc关闭流
as well as
也
FileReader.close()
does.
做。
回答by Csaba_H
According to BufferedReader source, in this case bReader.close call fReader.close so technically you do not have to call the latter.
根据 BufferedReader 来源,在这种情况下 bReader.close 调用 fReader.close 所以从技术上讲,您不必调用后者。
回答by Brian Agnew
The source code for BufferedReadershows that the underlying is closed when you close the BufferedReader.
BufferedReader的源代码显示,当您关闭 BufferedReader 时,底层是关闭的。
回答by McDowell
As others have pointed out, you only need to close the outer wrapper.
正如其他人指出的那样,您只需要关闭外包装。
BufferedReader reader = new BufferedReader(new FileReader(fileName));
There is a very slim chance that this could leak a file handle if the BufferedReader
constructor threw an exception (e.g. OutOfMemoryError
). If your app is in this state, how careful your clean up needs to be might depend on how critical it is that you don't deprive the OS of resources it might want to allocate to other programs.
如果BufferedReader
构造函数抛出异常(例如OutOfMemoryError
),这可能会泄漏文件句柄的可能性很小。如果您的应用程序处于此状态,则您的清理需要多小心可能取决于您不剥夺操作系统可能希望分配给其他程序的资源的重要性。
The Closeableinterface can be used if a wrapper constructor is likely to fail in Java 5 or 6:
所述可关闭的,如果一个封装构造是可能在Java 5或6失败界面可用于:
Reader reader = new FileReader(fileName);
Closeable resource = reader;
try {
BufferedReader buffered = new BufferedReader(reader);
resource = buffered;
// TODO: input
} finally {
resource.close();
}
Java 7 code should use the try-with-resourcespattern:
Java 7 代码应该使用try-with-resources模式:
try (Reader reader = new FileReader(fileName);
BufferedReader buffered = new BufferedReader(reader)) {
// TODO: input
}
回答by Jitendra
You Only Need to close the bufferedReader i.e reader.close() and it will work fine .
你只需要关闭 bufferedReader 即 reader.close() 就可以了。
回答by Anup Verma
After checking the source code, I found that for the example:
检查源代码后,我发现对于示例:
FileReader fReader = new FileReader(fileName);
BufferedReader bReader = new BufferedReader(fReader);
the close() method on BufferedReaderobject would call the abstract close() method of Readerclass which would ultimately call the implemented method in InputStreamReaderclass, which then closes the InputStreamobject.
BufferedReader对象上的 close() 方法将调用Reader类的抽象 close() 方法,该方法最终将调用InputStreamReader类中的已实现方法,然后关闭InputStream对象。
So, only bReader.close() is sufficient.
所以,只有 bReader.close() 就足够了。
回答by Claudiu
Starting from Java 7 you can use try-with-resources Statement
从 Java 7 开始,您可以使用try-with-resources 语句
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
return br.readLine();
}
Because the BufferedReader
instance is declared in a try-with-resource statement, it will be closed regardless of whether the try statement completes normally or abruptly. So you don't need to close it yourself in the finally
statement. (This is also the case with nested resource statements)
因为BufferedReader
实例是在 try-with-resource 语句中声明的,所以无论 try 语句是正常完成还是突然完成,它都会被关闭。所以你不需要在finally
语句中自己关闭它。(嵌套资源语句也是这种情况)
This is the recomanded way to work with resources, see the documentationfor more detailed information
这是使用资源的推荐方式,有关更多详细信息,请参阅文档
回答by Dmitry Gashko
I'm late, but:
我迟到了,但是:
BufferReader.java:
BufferReader.java:
public BufferedReader(Reader in) {
this(in, defaultCharBufferSize);
}
(...)
public void close() throws IOException {
synchronized (lock) {
if (in == null)
return;
try {
in.close();
} finally {
in = null;
cb = null;
}
}
}
回答by Omar Abdul'Azeez
You Don'tneed to close the wrapped reader/writer.
你不要需要关闭包裹读/写器。
If you've taken a look at the docs (Reader.close()
,Writer.close()
), You'll see that in Reader.close()
it says:
如果您查看了文档 ( Reader.close()
, Writer.close()
),您会在Reader.close()
其中看到:
Closes the stream and releases any system resources associated with it.
关闭流并释放与其关联的任何系统资源。
Which just says that it "releases any system resources associatedwith it". Even though it doesn't confirm.. it gives you a nudge to start looking deeper. and if you go to Writer.close()
it only states that it closes itself.
这只是说它“释放与之相关的任何系统资源”。即使它没有证实......它也会让你开始深入研究。如果你去Writer.close()
它,它只会说它会自行关闭。
In such cases, we refer to OpenJDKto take a look at the source code.
在这种情况下,我们参考OpenJDK来查看源代码。
At BufferedWriter Line 265you'll see out.close()
. So it's not closing itself.. It's something else. If you search the class for occurences of "out
" you'll notice that in the constructor at Line 87that out
is the writer the class wraps where it calls another constructor and then assigning out
parameter to it's own out
variable..
在 BufferedWriter Line 265你会看到out.close()
. 所以它并没有关闭自己..它是别的东西。如果你搜索类的“出现次数out
”你会发现,在构造函数中87号线即out
是作家的类包装它调用另一个构造函数,然后指定out
参数,将其自身的out
变量..
So.. What about others? You can see similar code at BufferedReader Line 514, BufferedInputStream Line 468and InputStreamReader Line 199. Others i don't know but this should be enough to assume that they do.
那么.. 其他人呢?您可以在BufferedReader Line 514、BufferedInputStream Line 468和InputStreamReader Line 199处看到类似的代码。其他我不知道,但这应该足以假设他们知道。