Java 7中引入的对try-with-resources的支持使我们能够声明将在try块中使用的资源,并确保在执行该块后将关闭资源。
简单地说,要自动关闭,必须在try中声明和初始化资源,如下所示:
try (PrintWriter writer = new PrintWriter(new File("test.txt"))) { writer.println("Hello World"); }
使用简单明显的try-with-resources代替传统和冗长的 try-catch-finally 块。
让我们比较以下代码示例–首先是一个典型的try-catch-finally块,然后是新方法,使用等效的try-with-resources块:
Scanner scanner = null; try { scanner = new Scanner(new File("test.txt")); while (scanner.hasNext()) { System.out.println(scanner.nextLine()); } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (scanner != null) { scanner.close(); } }
这是使用try-with-resources的超级简洁解决方案:
try (Scanner scanner = new Scanner(new File("test.txt"))) { while (scanner.hasNext()) { System.out.println(scanner.nextLine()); } } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(); }
你最钟意那种呢?
通过用分号分隔多个资源,可以在try-with-resources块中声明多个资源:
try (Scanner scanner = new Scanner(new File("testRead.txt")); PrintWriter writer = new PrintWriter(new File("testWrite.txt"))) { while (scanner.hasNext()) { writer.print(scanner.nextLine()); } }
要构造一个将由try-with-resources块正确处理的自定义资源,该类应需要实现Closeable或AutoCloseable接口,并覆盖close方法:
public class MyResource implements AutoCloseable { @Override public void close() throws Exception { System.out.println("Closed MyResource"); } }
资源首先将会被定义(创建),然后使用,最后被关闭。让我们看一下示例:
public class AutoCloseableResourcesFirst implements AutoCloseable { public AutoCloseableResourcesFirst() { System.out.println("Constructor -> AutoCloseableResources_First"); } public void doSomething() { System.out.println("Something -> AutoCloseableResources_First"); } @Override public void close() throws Exception { System.out.println("Closed AutoCloseableResources_First"); } }
public class AutoCloseableResourcesSecond implements AutoCloseable { public AutoCloseableResourcesSecond() { System.out.println("Constructor -> AutoCloseableResources_Second"); } public void doSomething() { System.out.println("Something -> AutoCloseableResources_Second"); } @Override public void close() throws Exception { System.out.println("Closed AutoCloseableResources_Second"); } }
private void orderOfClosingResources() throws Exception { try (AutoCloseableResourcesFirst af = new AutoCloseableResourcesFirst(); AutoCloseableResourcesSecond as = new AutoCloseableResourcesSecond()) { af.doSomething(); as.doSomething(); } }
Constructor -> AutoCloseableResources_First
Constructor -> AutoCloseableResources_Second
Something -> AutoCloseableResources_First
Something -> AutoCloseableResources_Second
Closed AutoCloseableResources_Second
Closed AutoCloseableResources_First
一个带有资源的try块仍然可以包含catch和finally块,其工作方式与传统try块相同。
在本文中,我们讨论了如何使用try-with-resources,如何用try-with-resources替换try,catch和finally,如何使用AutoCloseable构建自定义资源以及了解关闭资源的顺序。
关注笔者公众号,推送各类原创/优质技术文章 :arrow_down: