Java资源关闭 [英] Java resource closing
问题描述
try {
URLConnection connection = new URL(www.example.com) .openConnection();
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String response = rd.readLine();
rd.close();
} catch(异常e){
//异常处理
}
好吗?我的意思是,我关闭了BufferedReader在最后一行,但我不关闭InputStreamReader。应该从connection.getInputStream和独立的InputStreamReader中的BufferedReader创建独立的InputStreamReader,而不是关闭所有这两个读取器?
我认为最好把结束方法放在finally块中,如下所示:
InputStreamReader isr = null ;
BufferedReader br = null;
try {
URLConnection connection = new URL(www.example.com)。openConnection();
isr = new InputStreamReader(connection.getInputStream());
br = new BufferedReader(isr);
String response = br.readLine();
} catch(Exception e){
//异常处理
} finally {
br.close();
isr.close();
}
但它是丑陋的,因为关闭方法可以抛出异常,所以我有处理或抛出它。
哪个解决方案更好?或者什么是最好的解决方案?
Java中资源获取和释放的一般成语是:
final Resource resource = acquire();
try {
use(resource);
} finally {
resource.release();
}
注意:
-
try
应该立即跟随收购。这意味着你不能将其包装在装饰器中并保持安全(并且移除空格或将东西放在一行上无助于)。 - 每个
- 避免
null
,使用最终
。否则,你会有一些垃圾代码和潜在的NPE。 - 一般来说,除非有更多的资源关联,否则不需要关闭装饰器。但是,您通常需要刷新输出,但在异常情况下可以避免这种情况。
- 异常应该传递给调用者,或者从周围的
尝试
块(Java引导你误入歧途)
ou可以将这个废话用执行周围的成语,所以你不必重复自己(只写很多的样板)。
I'm writing an app that connect to a website and read one line from it. I do it like this:
try{
URLConnection connection = new URL("www.example.com").openConnection();
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String response = rd.readLine();
rd.close();
}catch (Exception e) {
//exception handling
}
Is it good? I mean, I close the BufferedReader in the last line, but I do not close the InputStreamReader. Should I create a standalone InputStreamReader from the connection.getInputStream, and a BufferedReader from the standalone InputStreamReader, than close all the two readers? I think it will be better to place the closing methods in the finally block like this:
InputStreamReader isr = null;
BufferedReader br = null;
try{
URLConnection connection = new URL("www.example.com").openConnection();
isr = new InputStreamReader(connection.getInputStream());
br = new BufferedReader(isr);
String response = br.readLine();
}catch (Exception e) {
//exception handling
}finally{
br.close();
isr.close();
}
But it is ugly, because the closing methods can throw exception, so I have to handle or throw it.
Which solution is better? Or what would be the best solution?
The general idiom for resource acquisition and release in Java is:
final Resource resource = acquire();
try {
use(resource);
} finally {
resource.release();
}
Note:
try
should immediately follow the acquire. This means you can't wrap it in the decorator and maintain safety (and removing spaces or putting things on one line doesn't help:).- One release per
finally
, otherwise it wont be exception safe. - Avoid
null
, usefinal
. Otherwise you'll have messy code and potential for NPEs. - Generally there is no need to close the decorator unless it has a further resource associated with it. However, you will generally need to flush outputs, but avoid that in the exception case.
- The exception should either be passed through to the caller, or caught from a surrounding
try
block (Java leads you astray here).
ou can abstract this nonsense with the Execute Around idiom, so you don't have to repeat yourself (just write a lot of boilerplate).
这篇关于Java资源关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!