使用 try with resource 时遇到 IDE 报错的问题分析及解决办法

*爱你&永不变心* 提交于 2020-02-28 06:29:49

问题描述:为了方便后续对某些 IO 流的操作,我写了某个工具类,里面封装了一个关闭 IO 流的方法。JDK 1.7 之前只能纯手写去封装一块关闭 IO 流的功能代码块,略微麻烦:

public static void copy(InputStream is, OutputStream os) {
	try {
		byte[] flush = new byte[1024 * 2];
		int readLen;
		while ((readLen = is.read(flush)) != -1) {
			os.write(flush, 0, readLen);
		}
		os.flush();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		close(is, os);
	}
}

public static void close(Closeable... ios) {
	for (int i = 0; i < ios.length; i++) {
		if (ios[i] != null) {
			try {
				ios[i].close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

JDK 1.7 及其之后的版本提供了 try with resource 这块语法糖, 编译器编译时会自动将其转化为 try{}catch(){}finally{} 的形式。我试着把上面的 close() 方法删除,又改成下面这样:

public static void copy(InputStream is, OutputStream os) {
	try(is; os) {  //try(is; os) {} ...中 is 和 os 两个引用变量被报错
		byte[] flush = new byte[1024 * 2];
		int readLen;
		while ((readLen = is.read(flush)) != -1) {
			os.write(flush, 0, readLen);
		}
		os.flush();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

没想到我的 IDE 报错了,提示:

Variable resource not allowed here for source level below 9

你妹的,不是 1.7 版本及以上版本就有 try with resource 这种写法了么?怎么我的 IDE 还是报错?还提示我需要 JDK 9 以上的版本!

查阅了一些博文,都写得不明不白的。要么说是因为使用 IDE 种类不同而产生了报错,要么说是 complier 版本要改成 1.7 及以上,然而没毛用,我项目编译器版本是 1.8 的,全局编译器版本更是 1.8 的,这根本不是版本错误的产生的问题。不同的 IDE ?你骗鬼哦?!我19年更新的 IDE 会识别不出 JDK 1.7 的这种语法?后面我又仔细想了下,将代码改成下面这种后 IDE 就不报错了:

public static void copy(InputStream is, OutputStream os) {
	try(InputStream is0 = is; OutputStream os0 = os) {
		byte[] flush = new byte[1024 * 2];
		int readLen;
		//下面的 is 和 os 可改成 is0 和 os0,也可不改,因为改不改都指向同一对象
		while ((readLen = is.read(flush)) != -1) {
			os.write(flush, 0, readLen);
		}
		os.flush();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

上面 try () 里的代码好别扭对不对?,本来想试试换成 JDK 9 及以上的版本再试试只写成 try(is; os) {...}... 这样行不行,但懒得下载 JDK 9 及以上的那些版本了,IDE 不会轻易犯傻,既然它提示了 Variable resource not allowed here for source level below 9 ,那么我就姑且相信它,9 及以上版本可以写成 try(is; os) 这种,在括弧里直接使用已经申明的引用型变量;而1.7 和 1.8 虽然已经有了 try with resource 语法,但在 try() 的括弧里一定要先单独申明 IO 对象的引用型变量,然后让其指向某个 IO 对象或等于 null(InputStream is0 = null,然而这样做几乎没有意义)才不会出错。

当然,没有尝试完就下结论可能有些草率。如果有尝试过 9 和 10 的 try with resource 语法的朋友,请留言告诉我^_^,我将不胜感激!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!