标签:tno resources tput lan new style utils 资源 try
try-catch-finally 常用,在异常关闭时应判断流是否为空
public class CloseableUtils { public static void closeable(Closeable ... closeIO) { for(Closeable clo:closeIO) { if(clo!=null) { try { clo.close(); } catch (IOException e) { System.out.println(DateUtils.getNowTime()+clo.getClass().getName()+"关闭发生异常"+e); } } } } }
try-with-resources ,它会自动关闭括号内的资源(resources),不用手动添加代码
注意:
1. resource 必须继承自 java.lang.AutoCloseable 2. 定义和赋值必须都在try里完成
try (FileOutputStream f = null;) { f = new FileOutputStream(new File("")); } catch (IOException e) { e.printStackTrace(); }
标签:tno resources tput lan new style utils 资源 try
原文地址:https://www.cnblogs.com/dc-earl/p/9160307.html