码迷,mamicode.com
首页 > 其他好文 > 详细

try(){}使用

时间:2019-11-08 19:03:23      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:keyword   file   sea   final   ble   try   cat   except   close   

InputStream is = null;

OutputStream os = null;
try {

} catch (IOException e) {

}finally{

try {

if(os!=null){

os.close();

}

if(is!=null){

is.close();
}
} catch (IOException e2) {

}

}

而现在你可以这样写:

 

 

try(

InputStream is = new FileInputStream("...");
OutputStream os = new FileOutputStream("...");

){
}catch (IOException e) {
}
 
 
 
 
try()里每个声明的变量类型都必须是Closeable的子类

try(){}使用

标签:keyword   file   sea   final   ble   try   cat   except   close   

原文地址:https://www.cnblogs.com/xguai/p/11822355.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!