其基本设想是把资源(比如文件或类似的东西)的作用域限定在代码块内,当程序离开这个代码块时,资源会被自动关闭;

要确保try-with-resources生效,正确的用法是为各个资源声明独立变量;

目前TWR特性依靠一个新定义的接口实现AutoCloseable;TWR的try从句中出现的资源类都必须实现这个接口;(并非所有的资源相关的类都采用了这项新技术;JDBC4.1已经具备了这个特性;)

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream; public class CoinTWR { @SuppressWarnings("null")
public static void main(String[] args) throws IOException {// 抛出异常 /** 资源管理 **/
try (FileInputStream fin = new FileInputStream("someFile.bin");
ObjectInputStream in = new ObjectInputStream(fin)) {
// ...
} /** 改善了错误跟踪的能力(注意其中被抑制的NullPointerException简称NPE)
* Ran As Java Application:
* Exception in thread "main" java.lang.NullPointerException
* at cointest.CoinTWR.main(CoinTWR.java:21)
*/
try (InputStream i = null) {
i.available();
} } }

1.3.4 try-with-resources (TWR)的更多相关文章

  1. java7新特性之Try-with-resources (TWR)

    java7新特性之Try-with-resources (TWR) This change is easy to explain, but it has proved to have hidden s ...

  2. Xamarin+Prism开发详解二:Xaml文件如何简单绑定Resources资源文件内容

    我们知道在UWP里面有Resources文件xxx.resx,在Android里面有String.Xml文件等.那跨平台如何统一这些类别不一的资源文件以及Xaml设计文件如何绑定这些资源?应用支持多国 ...

  3. [免费了] SailingEase .NET Resources Tool (.NET 多语言资源编辑器)

    这是我2010年左右,写 Winform IDE (http://www.cnblogs.com/sheng_chao/p/4387249.html)项目时延伸出的一个小项目. 最初是以共享软件的形式 ...

  4. [Android]使用自定义JUnit Rules、annotations和Resources进行单元测试(翻译)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5795091.html 使用自定义JUnit Rules.ann ...

  5. [Erlang 0122] Erlang Resources 2014年1月~6月资讯合集

    虽然忙,有些事还是要抽时间做; Erlang Resources 小站 2014年1月~6月资讯合集,方便检索.      小站地址: http://site.douban.com/204209/   ...

  6. [Erlang 0114] Erlang Resources 小站 2013年7月~12月资讯合集

    Erlang Resources 小站 2013年7月~12月资讯合集,方便检索.     附 2013上半年盘点: Erlang Resources 小站 2013年1月~6月资讯合集    小站地 ...

  7. sqoop:Failed to download file from http://hdp01:8080/resources//oracle-jdbc-driver.jar due to HTTP error: HTTP Error 404: Not Found

    环境:ambari2.3,centos7,sqoop1.4.6 问题描述:通过ambari安装了sqoop,又添加了oracle驱动配置,如下: 保存配置后,重启sqoop报错:http://hdp0 ...

  8. Resources.Load加载文件返回null的原因

    1.文件夹都要放在Resources目录下 2.加载时photoName不需要扩展名 Texture2D t = Resources.Load<Texture2D>("Loadi ...

  9. 严重: Error starting static Resources java.lang.IllegalArgumentException:

    严重: Error starting static Resources java.lang.IllegalArgumentException: Document base E:\myworkspace ...

  10. (转载)android:android.content.res.Resources$NotFoundException: String resource ID #..

    android.content.res.Resources$NotFoundException: String resource ID #0x0 找不到资源文件ID #0x0 原因分析如下: 遇到这种 ...

随机推荐

  1. oracle insert &字符插入问题

    例如执行一下语句: insert into NSRXT_SP (SP_ID, SP_TITLE, SP_DESC, SP_URL, SP_TYPE, SP_SUB_TYPE, ADD_TIME, CZ ...

  2. grunt serve Fatal error: Port 35729 is already in use by another process.

    y@y:~$ lsof | grunt y 0t0 TCP *: (LISTEN) Optimizin y 0t0 TCP *: (LISTEN) v8:Sweepe y 0t0 TCP *: (LI ...

  3. POJ2632 Crashing Robots(模拟)

    题目链接. 分析: 虽说是简单的模拟,却调试了很长时间. 调试这么长时间总结来的经验: 1.坐标系要和题目建的一样,要不就会有各种麻烦. 2.在向前移动过程中碰到其他的机器人也不行,这个题目说啦:a ...

  4. linux系统资源信息监控

    系统 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # ho ...

  5. config large memory

    C Configuring Large Memory Optimization This appendix provides information for configuring memory op ...

  6. HDU1863 畅通project 【最小生成树Prim】

    畅通project Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  7. xml--通过DOM解析XML

    此文章通过3个例子表示DOM方式解析XML的用法. 通过DOM解析XML必须要写的3行代码. step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器) step 2:获得具体的dom解 ...

  8. Cortex依赖管理

    cortex中文博客链接: http://cnblog.ctx.io/post/91333512673/cortex 关于cortex项目, 参见项目主页: http://ctx.io 项目在gith ...

  9. 拓扑排序 HDU1285

    这个题是个模板题,可以直接用拓扑排序的模板来做, AC代码 #include <stdio.h> #include<iostream> #include <string. ...

  10. MySQL 5.6.19编译安装

    1.创建mysql安装目录.mysql用户 [root@serv19 ~]# mkdir -p /u01/mysql/data [root@serv19 ~]# groupadd mysql [roo ...