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. Activity详解

    Activity是android应用的重要组成单元之一(另外3个是Service,BroadcastReceiver和ContentProvider).实际应用包含了多个Activity,不同的Act ...

  2. 多路径配置vlome group共享存储,VG的更新。

    1.  PV的概念: a)        一块物理磁盘一块物理硬盘在被LVM管理时被称为“物理卷”. b)        在LVM能对其进行管理之前需要在硬盘上产生一些特殊的数据结构,这个过程就是建立 ...

  3. SQL Server 2005 盛宴系列 经典教程

    SQL Server 2005 盛宴系列 经典教程  [复制链接]   发表于 2007-3-27 14:08 | 来自 51CTO网页 [只看他] 楼主     TECHNET  SQL serve ...

  4. springMVC get请求及其请求地址写法

    今天,需要写一个接口,写完之后,测试的时候发线一直报404错误,不知道为什么报错.应该是get请求地址的问题,get请求有两个参数,改为一个参数的时候是好用的,可能那种方式不适合写两个参数的get请求 ...

  5. hadoop安装与WordCount例子

    1.JDK安装 下载网址: http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u29-download-513648.html  ...

  6. 产生不重复的随机数TGUID

    uses ActiveX; procedure TForm1.BtnNewClick(Sender: TObject);var  ID: TGUID;  S: string;begin  if CoC ...

  7. MySQL Cluster测试过程中的错误汇总--ERROR 1296 (HY000)等等

    参考资料: http://dev.mysql.com/doc/refman/5.1/en/mysql-cluster-privilege-distribution.html http://www.cl ...

  8. Windows 7 Ultimate(旗舰版)SP1 32/64位官方原版下载(2011年5月12日更新版)

    MSDN于2011年5月12日,最新发布简体中文Windows 7 Ultimate 旗舰版 SP1 DVD镜像安装包,分32位和64位两个版本.最新发行代号分别是:677486(32位),67740 ...

  9. java_类泛型基本实例

    package ming; public class Apple2<T> { public T info; public Apple2(T info) { this.info = info ...

  10. How to Map Distinct Value Types Using Java Generics--reference

    原文:http://www.codeaffine.com/2015/03/04/map-distinct-value-types-using-java-generics/ Occasionally t ...