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

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

    public static void filyCopy(File one,File two){
FileInputStream fileInput = null;
FileOutputStream fileOutput = null;
try {
fileInput = new FileInputStream(one);
fileOutput = new FileOutputStream(two);
byte[] b = new byte[1024];
int len = 0;
while((len = fileInput.read(b)) != -1){
fileOutput.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {//释放资源
try {
if(fileInput != null){
fileInput.close();
}
if(fileOutput != null){
fileOutput.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}

使用 try-with-resources 语句来简化代码如下:

public static void filyCopy2(File one,File two){
try (FileInputStream fileInput = new FileInputStream(one);
FileOutputStream fileOutput = new FileOutputStream(two);){
byte[] b = new byte[1024];
int len = 0;
while((len = fileInput.read(b)) != -1){
fileOutput.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}
}

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

Java 7 新的 try-with-resources 语句,自动资源释放的更多相关文章

  1. java try(){}catch(){}自动资源释放

    从 Java 7 build 105 版本开始,Java 7 的编译器和运行环境支持新的 try-with-resources 语句,称为 ARM 块(Automatic Resource Manag ...

  2. Java SE7新特性之try-with-resources语句

       try-with-resources语句是一个声明一个或多个资源的 try 语句.一个资源作为一个对象,必须在程序结束之后随之关闭. try-with-resources语句确保在语句的最后每个 ...

  3. Java 7 新特性try-with-resources语句

    1.什么是try-with-resources语句 try-with-resources 语句是一个声明一个或多个资源的 try 语句.一个资源作为一个对象,必须在程序结束之后随之关闭. try-wi ...

  4. IBM Developer:Java 9 新特性概述

    Author: 成富 Date: Dec 28, 2017 Category: IBM-Developer (20) Tags: Java (27) 原文地址:https://www.ibm.com/ ...

  5. Java 8新特性终极指南

    目录结构 介绍 Java语言的新特性 2.1 Lambdas表达式与Functional接口 2.2 接口的默认与静态方法 2.3 方法引用 2.4 重复注解 2.5 更好的类型推测机制 2.6 扩展 ...

  6. Java 8 新特性终极版

    声明:本文翻译自Java 8 Features Tutorial – The ULTIMATE Guide,翻译过程中发现并发编程网已经有同学翻译过了:Java 8 特性 – 终极手册,我还是坚持自己 ...

  7. Effective Java 第三版——9. 使用try-with-resources语句替代try-finally语句

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  8. 【整理】Java 8新特性总结

    闲语: 相比于今年三月份才发布的Java 10 ,发布已久的Java 8 已经算是老版本了(传闻Java 11将于9月25日发布....).然而很多报道表明:Java 9 和JJava10不是 LTS ...

  9. 一小时上手Java 8新特性

    一小时上手Java 8新特性 本文摘译自 https://www.journaldev.com/2389/java-8-features-with-examples,并做了适当增补. Iterable ...

随机推荐

  1. React中的Statics对象

    statics 对象允许你定义静态的方法,这些静态的方法可以在组件类上调用.例如 var MyComponent = React.createClass({ statics: { customMeth ...

  2. 搭架私有Git服务器Gogs

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:搭架私有Git服务器Gogs.

  3. Android studio中出现非法字符时的部分解决方法

    我将原来在Eclipse中开发的工程搬到了Android studio上来,在运行,编译程序的过程中出现了错误.提示存在非法字符. 后来发现是由于程序代码中有中文字符的出现,问题就出在对中文支持的UT ...

  4. python会什么比c慢

    众所周知,python执行速度比c慢.原因为何? 先来看下面这张图: python的传统运行执行模式:录入的源代码转换为字节码,之后字节码在python虚拟机中运行.代码自动被编译,之后再解释成机器码 ...

  5. [React Native] Using the Image component and reusable styles

    Let's take a look at the basics of using React Native's Image component, as well as adding some reus ...

  6. Python 魔术方法指南

    入门 构造和初始化 构造定制类 用于比较的魔术方法 用于数值处理的魔术方法 表现你的类 控制属性访问 创建定制序列 反射 可以调用的对象 会话管理器 创建描述器对象 持久化对象 总结 附录 介绍 此教 ...

  7. MySQL计划任务(事件调度器)(Event Scheduler)

    http://www.cnblogs.com/c840136/articles/2388512.html https://dev.mysql.com/doc/refman/5.7/en/events- ...

  8. 应聘.net开发工程师常见的面试题(四)

    1.在Asp.net中所有的自定义用户控件都必须继承自________? 答:Control. 2.在.Net中所有可序列化的类都被标记为_____? 答:[serializable] 3.在.Net ...

  9. php开发环境以及插件的配置安装

    目前网上提供的常用的PHP集成环境主要有AppServ.phpStudy.WAMP和XAMPP等软件

  10. Pitfalls of the Hibernate Second-Level / Query Caches--reference

    This post will go through how to setup the Hibernate Second-Level and Query caches, how they work an ...