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

问题描述:为了方便后续对某些 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 就不报错了:

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,然而这样做几乎没有意义)才不会出错。

当然,没有尝试完就下结论可能有些草率。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容