java9 新特性六:try操作的升级
// java9 新特性六:try操作的升级
public static void main(String[] args) {
// java8 之前的资源关闭操作
// InputStreamReader reader = null;
// try {
// reader = new InputStreamReader(System.in);
// char[] cbuf = new char[20];
// int len;
// if ((len = reader.read(cbuf)) != -1) {
// String str = new String(cbuf, 0, len);
// }
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// if (reader != null) {
//
// try {
// reader.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
// java8当中资源的关闭操作:java8中,可以实现资源的自动关闭
// 要求自动关闭的资源的初始化必须放在try的一对小括号中
// try (InputStreamReader reader = new InputStreamReader(System.in)) {
// char[] cbuf = new char[20];
// int len;
// if ((len = reader.read(cbuf)) != -1) {
// String str = new String(cbuf, 0, len);
// System.out.println(str);
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// java9中资源的关闭操作:需要自动关闭的资源的实例化可以放在try的一对小括号外面的
// 此时的资源属性是一个常量,声明为final的,不可修改
InputStreamReader reader = new InputStreamReader(System.in);
try (reader){
char[] cbuf = new char[20];
int len;
if ((len = reader.read(cbuf)) != -1) {
String str = new String(cbuf, 0, len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}