zoukankan      html  css  js  c++  java
  • Java 7 新的 try-with-resources 语句,自动资源释放

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

    新的语句支持包括流以及任何可关闭的资源,例如,一般我们会编写如下代码来释放资源:

    ?

    private static void customBufferStreamCopy(File source, File target) {
        InputStream fis = null;
        OutputStream fos = null;
        try {
            fis = new FileInputStream(source);
            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();
        } finally {
            close(fis);
            close(fos);
        }
    }
      
    private static void close(Closeable closable) {
        if (closable != null) {
            try {
                closable.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    代码挺复杂的,异常的管理很麻烦。

    而使用 try-with-resources 语句来简化代码如下:在try中初始化, try 执行完毕后自动被关闭

    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 接口。

  • 相关阅读:
    Python批量 png转ico
    线性回归
    【论文集合】多语言机器翻译
    【论文集合】curriculum learning总结/课程学习论文
    crf++分词
    强化学习的探索策略方式
    关于multiprocessing中的logging的数据打印到本地失败或重复问题
    置信区间绘图、以10次平均值为例
    打印CSDN博客内容JS
    SUMO学习笔记(3)
  • 原文地址:https://www.cnblogs.com/kuyuyingzi/p/4266227.html
Copyright © 2011-2022 走看看