今天看前人代码的时候,发现了一个以前一直没用过的东西,

公司目前使用的是jdk1.8(vertx3需要),

在某处代码里有这样一行代码:

            try(
FileOutputStream fos=new FileOutputStream(fileDir+File.separator+fileName,false);
) {
...
} catch (IOException e) {
e.printStackTrace(); }

以前一直都是使用的try{}catch(){};但这里确是try(){}catch(){};

所以特地查了一下,发现:

总从jdk1.7后,java有一个这样的类:

java.lang.AutoCloseable.
源码中是这样描述该类的:
/**
* An object that may hold resources (such as file or socket handles)
* until it is closed. The {@link #close()} method of an {@code AutoCloseable}
* object is called automatically when exiting a {@code
* try}-with-resources block for which the object has been declared in
* the resource specification header. This construction ensures prompt
* release, avoiding resource exhaustion exceptions and errors that
* may otherwise occur.
*
* @apiNote
* <p>It is possible, and in fact common, for a base class to
* implement AutoCloseable even though not all of its subclasses or
* instances will hold releasable resources. For code that must operate
* in complete generality, or when it is known that the {@code AutoCloseable}
* instance requires resource release, it is recommended to use {@code
* try}-with-resources constructions. However, when using facilities such as
* {@link java.util.stream.Stream} that support both I/O-based and
* non-I/O-based forms, {@code try}-with-resources blocks are in
* general unnecessary when using non-I/O-based forms.
*
* @author Josh Bloch
* @since 1.7
*/

简单翻译就是:

在try() 中新建的文件流(或socket连接)对象,若该对象实现了接口java.lang.AutoCloseable,当try代码块结束的时候,该资源(文件流或socket连接)会被自动关闭.以达到自动释放资源,避免资源耗尽或报错的发生.

同时,注释中也表示,对于一些同时支持io和nio的类,比如java.util.stream.Stream,就无所谓使用哪种方式.

java源码的学习也很重要!

try}-with-resources的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  10. Sentiment Analysis resources

    Wikipedia: Sentiment analysis (also known as opinion mining) refers to the use of natural language p ...

随机推荐

  1. 单片机的基本构成、工作原理 LET′S TRY“嵌入式编程”: 1 of 6

    单片机的基本构成.工作原理 LET′S TRY“嵌入式编程”: 1 of 6 本连载讲解作为嵌入式系统开发技术人员所必需具备的基础知识.这些基础知识是硬件和软件技术人员都应该掌握的共通技术知识.有了电 ...

  2. ST算法(倍增)(用于解决RMQ)

    ST算法 在RMQ(区间最值问题)问题中,我了解到一个叫ST的算法,实质是二进制的倍增. ST算法能在O(nlogn)的时间预处理后,用O(1)的时间在线回答区间最值. f[i][j]表示从i位起的2 ...

  3. 编写高质量代码:改善Java程序的151个建议 --[117~128]

    编写高质量代码:改善Java程序的151个建议 --[117~128] Thread 不推荐覆写start方法 先看下Thread源码: public synchronized void start( ...

  4. Dubbo x Cloud Native 服务架构长文总结(很全)

    Dubbo x Cloud Native 服务架构长文总结(很全) mercyblitz SpringForAll社区 3天前 分享简介 Cloud Native 应用架构随着云技术的发展受到业界特别 ...

  5. 洛谷P4384 制胡窜

    这题TM是计数神题......SAM就是个板子,别脑残写错就完事了.有个技巧是快速定位子串,倍增即可. 考虑反着来,就是两个断点切割所有串,求方案数. 大概分类讨论一下......先特判掉一些情况.然 ...

  6. 【POJ2230】Watchcow

    题目大意:给定一个 N 个点,M 条边的无向图,要求不重复地经过每条边两次,并且从 1 号节点出发最后回到 1 号节点,求一条路径. 题解:不重复地经过两次这个操作很容易地通过无向图的建边方式来实现, ...

  7. 【洛谷P4145】花神游历各国

    题目大意:给定一个长度为 N 的序列,支持区间开根,区间求和. 题解:对于区间开根操作,可以发现任何一个位置的值开根至多 6 次就会变成 1.因此即使是整个区间开根,暴力修改6次后,所有的点的权值均小 ...

  8. linux 系统调用之文件操作

    fcntl 文件控制 open 打开文件 creat 创建新文件 close 关闭文件描述字 read 读文件 write 写文件 readv 从文件读入数据到缓冲数组中 writev 将缓冲数组里的 ...

  9. 你真的知道什么是【共享Session】,什么是【单点登录】吗?

    一直有人问,为什么我实现的共享session不能单点登录,今天我也抽时间准备好好说一下. 我要喷(别喷我) 首先,网上水货文章很多,CSDN居多.CSDN转载率很高,也就是说同相同文章有很多,换汤不换 ...

  10. msvcp100d.dll文件丢失,解决找不到msvcp100d.dll的问题

    http://www.jb51.net/dll/msvcp100d.dll.html msvcp100d.dll控件常规安装方法(仅供参考): 一.如果在运行某软件或编译程序时提示缺少.找不到msvc ...