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. grdgradient

    from http://gmt.soest.hawaii.edu/doc/5.2.1/grdgradient.html grdgradient grdgradient - Compute direct ...

  3. 原生js操作cookie

    写cookie function setCookie(name,value) { var Days = 30; var exp = new Date(); exp.setTime(exp.getTim ...

  4. error when loading the sdk 发现了元素 d:skin 开头无效内容

    把devices.xml这个文件删除,再把sdk里面tools\lib下的这个文件复制到你删除的那个目录里.重新启动eclipse

  5. Android下得到已安装Application信息

    在上一篇blog中,谈到如何利用APK archive文件得到相应信息.(当时发现例如ProcessName,DataDir等信息,其实是无法得到的). 当前咱们看看如何通过系统取得已经安装的Appl ...

  6. Mysql 5.5 replication 多数据库主从备份Master-Slave配置总结

    配置Mysql server 5.5 的双机备份,也就是master-slave模式.本例子还是一个多database复制的情况. 现在有两个database在同一台mysql server,也就是m ...

  7. [Javascript + lodash] sortBy and sortedIndex

    sortBy: var collection = ['John', 'Petteri', 'Antti', 'Joonas', 'Zhentian']; var sorted = _.sortBy(c ...

  8. android学习日记07--Canvas画布

    1.Canvas Canvas类主要实现了屏幕的绘制过程,其中包含了很多实用的方法,比如绘制一条路径.区域.贴图.画点.画线.渲染文本,当然Android官网提示大家很多方法有不同的重载版本,参数更灵 ...

  9. android学习日记03--常用控件ListView

    常用控件 8.ListView 列表视图,比如游戏的排行榜.列表数据可以根据屏幕大小自适应 列表的显示需要三个元素: a.ListVeiw:用来展示列表的View. b.适配器:用来把数据映射到Lis ...

  10. Windows下python环境变量配置

    默认情况下,在windows下安装python之后,系统并不会自动添加相应的环境变量.此时不能在命令行直接使用python命令. 1. 首先需要在系统中注册python环境变量:假设python的安装 ...