try-with-resource的输入输出流自动关闭
最近在做代码审核的时候,审核工具提示我将 try-catch-finally 给替换掉,而且根据公司相关要求,该提示的级别还不低,不改不予通过。
先看看代码吧:
filereader fr = null; bufferedreader br = null; try { fr = new filereader(filename); br = new bufferedreader(fr); return br.readline(); } catch (exception e) { log.error("error:{}", e); } finally { if (br != null) { try { br.close(); } catch(ioexception e){ log.error("error:{}", e); } } if (fr != null ) { try { br.close(); } catch(ioexception e){ log.error("error:{}", e); } } }
审核工具给出的意见是 替换为:
try ( filereader fr = new filereader(filename); bufferedreader br = new bufferedreader(fr) ) { return br.readline(); }catch (exception e) { log.error("error:{}", e); }
或者是:
try ( bufferedreader br = new bufferedreader(new filereader(filename)) ) { // no need to name intermediate resources if you don't want to return br.readline(); } catch (exception e) { log.error("error:{}", e); }
对比代码,不难发现,输入输出流的关闭存在着差异。难道输入输出流不用关闭了吗?
带着这个问题看看源代码,发现
public class fileinputstream extends inputstream{} public abstract class inputstream implements closeable {} /** * a {@code closeable} is a source or destination of data that can be closed. * the close method is invoked to release resources that the object is * holding (such as open files). * * @since 1.5 */ public interface closeable extends autocloseable {} /** * an object that may hold resources (such as file or socket handles) * until it is closed. the {@link #close()} method of an {@code autocloseable} * object is called automatically when exiting a {@code * try}-with-resources block for which the object has been declared in * the resource specification header. this construction ensures prompt * release, avoiding resource exhaustion exceptions and errors that * may otherwise occur. * * @apinote * <p>it is possible, and in fact common, for a base class to * implement autocloseable even though not all of its subclasses or * instances will hold releasable resources. for code that must operate * in complete generality, or when it is known that the {@code autocloseable} * instance requires resource release, it is recommended to use {@code * try}-with-resources constructions. however, when using facilities such as * {@link java.util.stream.stream} that support both i/o-based and * non-i/o-based forms, {@code try}-with-resources blocks are in * general unnecessary when using non-i/o-based forms. * * @author josh bloch * @since 1.7 */ public interface autocloseable {}
autocloseable 顾名思义, 自动关闭流. 从注释中我们可以发现,实现了autocloseable并在try()中声明的对象,当try-with-resource代码块执行完的时候,会自动调用close()方法。
注意:
一个 try-with-resources 语句可以像普通的 try 语句那样有 catch 和 finally 块。在try-with-resources 语句中, 任意的 catch 或者 finally 块都是在声明的资源被关闭以后才运行。
使用try-with-resource需要注意的地方
try-with-resource是jdk7引入的语法糖,可以简化autocloseable资源类的关闭过程,
比如jdk7以前下面的代码:
file file = new file("d:/tmp/1.txt"); fileinputstream fis = null; try { fis = new fileinputstream(file); xxxxx xxxxx } catch (ioexception e) { e.printstacktrace(); }finally{ if(fis != null){ try { fis.close(); } catch (ioexception e) { e.printstacktrace(); } } }
上面是一段读取文件内容的示意代码,为了防止在try代码块中出现异常后导致的资源泄露问题,在finally代码块中一般处理资源的关闭事项。
jdk之后上面的代码就可以简化成下面的写法:
file file = new file("d:/tmp/1.txt"); try(fileinputstream fis = new fileinputstream(file);) { fis.read(); } catch (ioexception e) { e.printstacktrace(); }finally{ }
可以看出是简化了不少,之所以称之为语法糖,是因为编译成class文件后实际的代码就不是这样的了,编译过程中会自动添加资源的关闭处理。
上面的代码编译出的class文件使用javap进行反编译后是下面这样的
file file = new file("d:/tmp/1.txt"); try { throwable var2 = null; object var3 = null; try { fileinputstream fis = new fileinputstream(file); xxx xxxx } catch (throwable var12) { if (var2 == null) { var2 = var12; } else if (var2 != var12) { var2.addsuppressed(var12); } throw var2; } } catch (ioexception var13) { var13.printstacktrace(); }
好了,上面已经引入今天的主题,try-with-resource,但是仍然有需要注意的地方。
比如下面的代码:
private static class myresource implements autocloseable{ private myresource1 res; public myresource(myresource1 res){ this.res = res; } @override public void close() throws exception { system.out.println("myresource自动关闭"); integer a = null; a.tostring(); this.res.close(); } } private static class myresource1 implements autocloseable{ @override public void close() throws exception { system.out.println("myresource1自动关闭"); } } @test public void test() throws exception{ try( myresource r = new myresource(new myresource1())){ integer a = null ; a.tostring(); } }
执行上面的代码,由于myresource的close方法中出现了异常,此时创建的myresource1就不会被关闭,从而出现资源泄露情况,为了规避这个问题,为了规避这个问题,我们需要创建的实现autocloseable接口的对象单独创建。
如下面所示:
try( myresource1 res= new myresource1(); myresource r = new myresource(res)){ integer a = null ; a.tostring(); }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。
需要了解更多c/c++开发分享使用try-with-resource的输入输出流自动关闭,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/678287.html