technology-note/java/其他/4.java文本,网络等资源自动回收.md
2019-04-08 10:07:19 +08:00

60 lines
2.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
id: '2019-03-25-20-52'
date: '2019/03/25 20:52'
title: '更优雅的关闭java文本、网络等资源'
tags: ['java', 'InputStream', 'OutputStream', 'socket', 'try-with-resource']
categories:
- 'java'
- 'java基础'
---
  通常在 java 中对文本、网络资源等操作起来是很繁杂的,要声明,读取,关闭三个阶段,还得考虑异常情况。假设我们要读取一段文本显示到控制台,通常会有如下的代码:
```java
public static void main(String[] args) {
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream("./pom.xml");
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader reader = new BufferedReader(inputStreamReader);
String str;
while ((str = reader.readLine()) != null) {
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (Exception e) {
}
}
}
}
```
<!-- more -->
在 finally 的关闭代码中,还要再来一个 try/catch看着是不是很难受很不优雅很想干掉这个 finally
&emsp;&emsp;自从 java7 以来这个问题已经有比较好的解决办法了,那就是**try-with-resource**,可能是 jdk 开发人员也觉得之前的关闭资源写法太反人类,所以做了这样的一个语法糖。注意这并不是什么新特性,只是一个语法糖,简化代码的。如果你反编译代码后会发现还是 try/catch/finally 的传统写法。
&emsp;&emsp;try-with-resource 用法如下:
```java
try (FileInputStream inputStream = new FileInputStream("./pom.xml")) {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
String str;
while ((str = reader.readLine()) != null) {
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
}
```
无需在 finally 中手动关闭 inputStream凡是实现了 AutoCloseable 接口的,且在 try 后面的括号中创建的,都会在 try/catch 执行完毕后确保调用 close 方法。这么写是不是优雅多了??
**本文原创发布于:**[http://www.tapme.top/blog/detail/2019-03-25-20-52](http://www.tapme.top/blog/detail/2019-03-25-20-52)