线上代码,如何正确的关闭BufferedReader流。我用的JDK1.7
原来的代码如下:
public static String httpPostWithJson(String ecUrl, String params) {
try {
// 创建连接
URL url = new URL(ecUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.connect();
// POST请求
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(params);
out.flush();
out.close();
// 读取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sb.append(lines);
}
// System.out.println(sb);
reader.close();
// 断开连接
connection.disconnect();
return sb.toString();
} catch (MalformedURLException e) {
logger.error("httpPostWithJsonMalformedURLException error", e);
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
logger.error("httpPostWithJsonUnsupportedEncodingException error", e);
e.printStackTrace();
} catch (IOException e) {
logger.error("httpPostWithJsonIOException error", e);
e.printStackTrace();
}
return null;
}
在上述代码中,流的关闭是放在try中的,但是我们都知道流资源的关闭尽量要放在finally块中,因为如果try中代码执行失败,流没有被正确关闭就造成资源浪费。因为我将流的关闭移到finally块中如下:
public static String httpPostWithJson(String ecUrl, String params) {
BufferedReader reader = null;
HttpURLConnection connection = null;
try {
URL url = new URL(ecUrl);
connection = (HttpURLConnection) url.openConnection();
// 创建连接
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.connect();
// POST请求
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(params);
out.flush();
out.close();
// 读取响应
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sb.append(lines);
}
return sb.toString();
} catch (MalformedURLException e) {
logger.error("httpPostWithJsonMalformedURLException error", e);
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
logger.error("httpPostWithJsonUnsupportedEncodingException error", e);
e.printStackTrace();
} catch (IOException e) {
logger.error("httpPostWithJsonIOException error", e);
e.printStackTrace();
} finally {
try {
if (null != reader) {
reader.close();
}
if (null != connection) {
connection.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
这样虽然在finally中关闭了流,但是又要在finally中引入IOException,这样是不是很麻烦啊
关于关闭流,还有没有好的措施或者实践经验呢?
谢谢~~
补充:JDK 1.7但是不能用Try-with-resources机制
这个问题其实无须过多困扰。也没有必要往JDK1.7的try-with-resources上扯。
首先关闭资源放在try块里一定会有问题:资源可能不被关闭。
所以资源的关闭应该放在finally里,这没有什么疑问。
至于finally块里close资源会额外引入
IOE
,这也是无法避免的。目前(就我见到过的)绝大多数代码里,捕获
IOE
后,最多打一条log,更多的是noop,即no operations,do nothing。close的时候IOE发生的几率很小,它应该属于一种操作系统层面的error,选择忽略它是正确的选择,毕竟你的系统不能因为一个资源关闭错误而停止运行。况且,如果你硬要捕获这个IOE,那能做些什么呢。
如果不想在finally块里引入try-catch,我见过guava的一种关闭方式,写个工具方法叫做closeQuietly(),不吵不闹就挺好。