线上代码,如何正确的关闭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机制

fd46814e11e97b8396cd0889261bed94.png

a85d8fa3efdfd217f0dd588b092b13b3.png

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐