首先,我们看下面的代码。
BufferedReader br = null; try { br = new BufferedReader(new FileReader("test.txt")); br.readLine(); } catch (Exception e) { e.printStackTrace(); } finally { if (br != null) try { br.close(); } catch (IOException e) { e.printStackTrace(); } }
有些资源使用完是需要释放的,通常我们在 finally 语句中做这件事情。这样的代码看上去会很乱,要是 Java 能帮我们自动释放资源就好了。我们的美好愿望实现了,Java SE 7 引入了 try-with-resources 语句,只要你的类实现了接口 java.lang.AutoCloseable, java 自动帮我们调用 close 方法释放资源,看看下面的代码吧,是不是非常整洁。
try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) { br.readLine(); } catch (Exception e) { e.printStackTrace(); }
http://blog.csdn.net/shangboerds/article/details/49493721
更加详细:
相关推荐
主要介绍了Java使用 try-with-resources 实现自动关闭资源的方法,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
主要介绍了Java如何优雅地关闭资源try-with-resource及其异常抑制,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Try-with-resources-Cwiczenie
Redis分布式锁的try-with-resources实现 一、简介 在当今这个时代,单体应用(standalone)已经很少了,java提供的synchronized已经不能满足需求,大家自然 而然的想到了分布式锁。谈到分布式锁,比较流行的方法有3...
The entire Java language, including Java 7-specific features such as switch on string, try-with-resources, final rethrow, multicatch, and SafeVarargs A huge assortment of Java 7 APIs, beginning with ...
[第2讲]变量配置flv,[第2讲]变量配置
如何使用 try-with-resources 代替try-catch-finally? I/O 什么是序列化?什么是反序列化? Java 序列化中如果有些字段不想进行序列化,怎么办? 获取用键盘输入常用的两种方法 Java 中 IO 流分为几种? 既然有了字节...
try-with-resources 语句中的资源只能是实现了java.lang.AutoCloseable接口的类实例,但是 Java SE 7 之後的所有实现了java.io.Closeable的类都实现了java.lang.AutoCloseable接口(该接口是在Java SE 7中才引入的)...
The ability to use a try-with-resources statement to automatically close resources of type Connection, ResultSet, and Statement; see Closing Connections in Processing SQL Statements. RowSet 1.1: The ...
To list a few noticeable enhancements, Java 7 includes support for strings in switch statements, try-with-resources statement, improved multi-catch, binary numeric literals, numeric literals with ...
解释说明 1.tar文件准确的说是打包文件,将文件打包到一个tar文件中,文件名后缀是.tar 2.Gzip是将文件的存储空间压缩保存,文件名...下文代码中的流操作使用了try-with- resources语法,所以不用写代码手动的close流。
//try-with-resources语法,不用手动的编码关闭流 try (Stream<String> lines = Files.lines( filePath )) { lines.forEach(System.out::println); } catch (IOException e) { e.printStackTrace();//只是测试...
其中对于流的关闭,jdk1.7以上还实现了try-with-resources语句,其原理与上述方法相同。 事务的操作以及数据库锁机制: Spring框架提供了编程式事务管理和声明式事务管理 编程式事务管理: 可以清楚地控制事务的...
process方法用于处理日志文件,它使用了try-with-resources语句来确保资源被正确关闭。在该方法中,创建了一个FileWriter对象用于写入日志文件,并使用PrintWriter对象进行格式化输出。如果在处理过程中发生异常,会...
在SQL Server的内部存储中,日期和时间不是以字符串的形式存储的,而是使用整数来存储的。这篇文章主要介绍了SQL Server 日期和时间的内部存储,需要的朋友可以参考下
介绍了C#编写windows计算器的代码,有需要的朋友可以参考一下
Java 9 Revealed is for experienced Java programmers looking to make the migration from Java 7 or Java 8 to Java 9.Author Kishori Sharan begins by covering how to develop Java applications using new ...
JDK1.7新特性介绍 1. 对Java集合(Collections)的...7. 增加了try-with-resources语句,确保每个资源都在生命周期结束后被关闭 8. 使用泛型增加了类型推断机制 9. 增加了fork/join框架来增强对处理多核并行计算的支持
Java 中的语法糖,真甜 语法糖 泛型 自动拆箱和自动装箱 枚举 内部类 变长参数 增强 for 循环 Switch 支持字符串和枚举 条件编译 断言 try-with-resources 字符串相加 学习语法糖的意义
try-with-resources,之前使用的很多IO操作都需要手动去关闭流,现在使用这种方式,只要是实现了java.lang.AutoCloseable和java.io.Closeable的类就不需要再手动关闭了。 例子:try(OutputStream out=new ...