从 Java 7 build 105 版本开始,Java 7 的编译器和运行环境支持新的 try-with-resources 语句,称为 ARM 块(Automatic Resource Management) ,自动资源管理。

使用try(){}catch(){}效果:

private static void customBufferStreamCopy(File source, File target) {
    try (InputStream fis = new FileInputStream(source);
        OutputStream fos = new FileOutputStream(target)){
  
        byte[] buf = new byte[8192];
  
        int i;
        while ((i = fis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

在这个例子中,数据流会在 try 执行完毕后自动被关闭,前提是,这些可关闭的资源必须实现 java.lang.AutoCloseable 接口。

相关文章:

  • 2021-07-16
  • 2021-11-15
  • 2022-12-23
  • 2022-12-23
  • 2022-01-31
  • 2021-08-19
  • 2022-12-23
  • 2022-03-04
猜你喜欢
  • 2021-08-27
  • 2021-08-05
  • 2021-10-28
  • 2021-10-07
  • 2021-07-30
相关资源
相似解决方案