1.3.4 try-with-resources (TWR)
其基本设想是把资源(比如文件或类似的东西)的作用域限定在代码块内,当程序离开这个代码块时,资源会被自动关闭;
要确保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)的更多相关文章
- java7新特性之Try-with-resources (TWR)
java7新特性之Try-with-resources (TWR) This change is easy to explain, but it has proved to have hidden s ...
- Xamarin+Prism开发详解二:Xaml文件如何简单绑定Resources资源文件内容
我们知道在UWP里面有Resources文件xxx.resx,在Android里面有String.Xml文件等.那跨平台如何统一这些类别不一的资源文件以及Xaml设计文件如何绑定这些资源?应用支持多国 ...
- [免费了] SailingEase .NET Resources Tool (.NET 多语言资源编辑器)
这是我2010年左右,写 Winform IDE (http://www.cnblogs.com/sheng_chao/p/4387249.html)项目时延伸出的一个小项目. 最初是以共享软件的形式 ...
- [Android]使用自定义JUnit Rules、annotations和Resources进行单元测试(翻译)
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5795091.html 使用自定义JUnit Rules.ann ...
- [Erlang 0122] Erlang Resources 2014年1月~6月资讯合集
虽然忙,有些事还是要抽时间做; Erlang Resources 小站 2014年1月~6月资讯合集,方便检索. 小站地址: http://site.douban.com/204209/ ...
- [Erlang 0114] Erlang Resources 小站 2013年7月~12月资讯合集
Erlang Resources 小站 2013年7月~12月资讯合集,方便检索. 附 2013上半年盘点: Erlang Resources 小站 2013年1月~6月资讯合集 小站地 ...
- 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 ...
- Resources.Load加载文件返回null的原因
1.文件夹都要放在Resources目录下 2.加载时photoName不需要扩展名 Texture2D t = Resources.Load<Texture2D>("Loadi ...
- 严重: Error starting static Resources java.lang.IllegalArgumentException:
严重: Error starting static Resources java.lang.IllegalArgumentException: Document base E:\myworkspace ...
- (转载)android:android.content.res.Resources$NotFoundException: String resource ID #..
android.content.res.Resources$NotFoundException: String resource ID #0x0 找不到资源文件ID #0x0 原因分析如下: 遇到这种 ...
随机推荐
- bit和sbit的区别
1.bit和sbit都是C51扩展的变量类型. bit和int char之类的差不多,只不过char=8位, bit=1位而已.都是变量,编译器在编译过程中分配地址.除非你指定,否则这个地址是随机的. ...
- Facebook 开源三款图像识别人工智能软件
Facebook今天开源了三款人工智能图像分割(Image Segmentation)软件,分别是DeepMask.SharpMask和MultiPathNet,三款工具相互配合完成一个完整的图像识别 ...
- magento 修改 paypal order product name
app/code/core/Mage/Sales/Model/Quote Item.php public function setProduct($product) { if ($this-> ...
- MapReduce Shuffle And Sort
引言 MapReduce作出保证:进入每个Reducer的数据行都是有序的(根据数据行的键值进行排序).MapReduce将Mapper的输出进行排序并传递给Reducer作为输入的过程称为Shu ...
- delphi调用java编写的webservice
delphi调用java编写的webservice JAVApojo: public class GroupInfo implements Serializable{ private stati ...
- DLL入门浅析(3)——从DLL中导出变量
转载自:http://www.cppblog.com/suiaiguo/archive/2009/07/20/90643.html 前面介绍了怎么从DLL中导出函数,下面我们来看一下如何从DLL中导出 ...
- Selenium webdirver 操作浏览器
打开浏览器 HtmlUnit Driver 优点:不会实际打开浏览器,运行速度很快. 缺点:对JavaScript的支持不够好,有时会捕获不到页面元素. 使用:WebDriver driver=new ...
- java异常面试常见题目
在Java核心知识的面试中,你总能碰到关于 处理Exception和Error的面试题.Exception处理是Java应用开发中一个非常重要的方面,也是编写强健而稳定的Java程序的关键,这自然使它 ...
- angularjs改变路由时控制器每次都执行两次
原因如下: 代码里面也初始化了控制器 模板配置了一个控制器
- AFNetworking (3.1.0) 源码解析 <二>
这次讲解AFHTTPSessionManager类,按照顺序还是先看.h文件,注释中写到AFHTTPSessionManager是AFURLSessionManager的子类,并且带有方便的HTTP请 ...