解决try catch finally中close的问题
2016-07-21 15:07
302 查看
解决try catch finally中close的问题
一直觉得要在finally里关闭流,是很讨厌的,因为代码会非常糟糕。然后在SO中看到了一个JDK7+的方法:try (BufferedReader br = new BufferedReader(new FileReader(path))) { return br.readLine(); }
The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.
只有实现了AutoClosable接口,才可以被当作一个资源使用。从反编译来看,的确实现了AutoClosable接口。
相关文章推荐
- linux 文件类型
- hdu 2087剪花布条
- JAVA反射机制
- leetcode 260
- Android如何遍历特定目录下所有文件
- MyEclipse中点击Deploy MyEclipse J2EE Project to Server没有反应解决方法
- LR提示Start Recording Error
- android LayoutInflater用法和源码解析
- 使用七牛云存储实现Android版本更新
- 在Spring-MVC中使用logback和slf4j
- Qt5中QPrinter类和QPinterDialog类
- angularJS promise $q
- Android技术积累:开发规范
- /var子目录
- LINUX 目录结构
- 如何在Android Studio添加本地aar包引用
- 算法研究、编程艺术、红黑树、数据挖掘、程序员面试5大系列集锦
- SVN服务器搭建
- 配置Java环境
- git修改提交作者和邮箱