本篇内容介绍了“java中怎么使用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是JDK7引入的语法糖,可以简化Autocloseable资源类的关闭过程,
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代码块中一般处理资源的关闭事项。
File file = new File("d:/tmp/1.txt"); try(FileInputStream fis = new FileInputStream(file);) { fis.read(); } catch (IOException e) { e.printStackTrace(); }finally{ }
可以看出是简化了不少,之所以称之为语法糖,是因为编译成class文件后实际的代码就不是这样的了,编译过程中会自动添加资源的关闭处理。
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(); }
“java中怎么使用try-with-resource实现输入输出流自动关闭”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。