使用try-with-resource自动关流

使用try-with-resource自动关流


```java
try(BufferedInputStream bis=new BufferedInputStream(new FileInputStream("D:/img/a.jpg"));
    BufferedOutputStream bos=   new BufferedOutputStream(new FileOutputStream("D:/img/b.jpg")))
{
  int b=-1;
  while((b=bis.read())!=-1){
  bos.write(b);
}
  bos.flush();
}catch(IOException io){

 log.info("文件读取写出异常 ",e)
}

再也不用自己手动在finally块关流了。有时候忘记关,流水线都跑不起来。
这样的代码简洁而美观。可读性强。

猜你喜欢

转载自blog.csdn.net/weixin_42328375/article/details/105288547