java se1.7 try-with-resources 使用
来源:互联网 发布:剑灵灵族捏脸数据图 编辑:程序博客网 时间:2024/06/13 11:59
在进行java编程的过程中,出现文件流未关闭或者connection未关闭的情况,很难发现而且出现的问题也不知道是哪里的问题,无意中发现javase1.7开始出现try-with-resources,可以很方便的为我们处理忘记关闭文件资源的情况。
String path = "C:\\Users\\ASUS\\Desktop\\11.txt";
try(BufferedReader br = new BufferedReader(new FileReader(path))){
System.out.println(br.readLine().toString());
} catch (FileNotFoundException e) {
System.out.println(1);
e.printStackTrace();
} catch (IOException e) {
System.out.println(2);
e.printStackTrace();
} finally {
System.out.println(3);
}
try(Connection con = DBUtil.getConnection()){
System.out.println(8888);
} catch (SQLException e) {
System.out.println(11);
e.printStackTrace();
} finally {
System.out.println(22);
}
书写比常规的try-catch要简单很多
注:java.lang.AutoCloseable
或java.io.Closeable
的对象都可以做为资源自动关闭
- java se1.7 try-with-resources 使用
- Java 7中的Try-with-resources
- Java 7中的try-with-resources语句
- Java 7中的Try-with-resources
- Java 7中的Try-with-resources
- java try-with-resources 语句
- Java 7 try-with-resources自动关闭资源
- java 7新特性-TWR(Try-with-resources)
- Java 7 新特性try-with-resources语句
- 使用try-with-resources自动释放资源
- 使用Try-with-resources自动关闭资源
- java新特性之try-with-resources
- Java编程——try-with-resources
- java炒冷饭系列08 try-with-resources
- JAVA【IO三】try-with-resources
- Jdk1.7(及以上) 使用 try-with-resources 替代try-catch-finally
- Java 7 新的 try-with-resources 语句,自动资源释放 try(){} catch(){}
- Java SE 7 中带资源声明的 try 语句(try-with-resources statement)
- 51C中_nop_(); 指令的说明和作用
- static 和 self的区别
- win10 + vs2013 +caffe 简易CPUonly安装步骤
- Common lisp编译成C可执行文件
- IDEA开发spring mvc 报404错误
- java se1.7 try-with-resources 使用
- C语言(常量与变量)
- Fibonacci(矩阵快速幂模板)
- maven build后Downloading maven-metadata.xml 的解决方法
- unity3D 制作3D塔防游戏(二) 制作敌人行走路径 无限循环
- 接口 修饰符 继承 变量命名规范
- UESTC 1051 Eggs broken【思维+期望Dp】
- TCP / Http区别
- RecycleView的监听事件