当前位置: 首页 > news >正文

莱州网站建设教程合肥网站优化方案

莱州网站建设教程,合肥网站优化方案,局域网网站建设工具,做的网站如何放在电脑上文章目录 java中try-with-resources自动关闭io流0 简要说明try-with-resources java中try-with-resources自动关闭io流 0 简要说明 在传统的输入输出流处理中,我们一般使用的结构如下所示,使用try - catch - finally结构捕获相关异常,最后不…

文章目录

  • java中try-with-resources自动关闭io流
    • 0 简要说明
    • try-with-resources

java中try-with-resources自动关闭io流

0 简要说明

在传统的输入输出流处理中,我们一般使用的结构如下所示,使用try - catch - finally结构捕获相关异常,最后不管是否有异常,我们都将流进行关闭处理

  try {//业务代码} catch (SQLException e) {// 异常,数据回滚try {connection.rollback();} catch (SQLException ex) {log.error(ERROR_MSG, e);throw new IllegalArgumentException(e);}log.error(ERROR_MSG, e);throw new IllegalArgumentException(e);} finally {if (Objects.nonNull(preparedStatement)) {try {preparedStatement.close();} catch (SQLException e) {log.error(ERROR_MSG, e);throw new IllegalArgumentException(e);}}if (Objects.nonNull(connection)) {try {connection.close();} catch (SQLException e) {log.error(ERROR_MSG, e);throw new IllegalArgumentException(e);}}}

try-with-resources

在jdk1.7之后,推荐使用try() {} catch(IOException e){}的方式来处理io流,它可以自动关闭流。如下所示,是一个简单的按行读取文件内容的示例
 @Testpublic void readFileContent() {File file = new File("d://DumpStacks.log");try (BufferedReader reader = new BufferedReader(new FileReader(file))) {String line;while ((line = reader.readLine()) != null) {log.info("line : 【{}】" , line);}} catch (IOException e) {log.error("error msg : 【{}】", e);throw new RuntimeException(e);}}

抛异常情况

java.lang.RuntimeException: java.io.FileNotFoundException: d:\DumpStacks.log (系统找不到指定的文件。)

at com.geekmice.springbootselfexercise.NoDaoTest.readFileContent(NoDaoTest.java:347)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

Caused by: java.io.FileNotFoundException: d:\DumpStacks.log (系统找不到指定的文件。)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.(FileInputStream.java:138)
at java.io.FileReader.(FileReader.java:72)
at com.geekmice.springbootselfexercise.NoDaoTest.readFileContent(NoDaoTest.java:340)
在这里插入图片描述

反编译之后可以看到自动关闭流操作

打开target,找到对应字节码文件
在这里插入图片描述

 @Testpublic void readFileContent() {File file = new File("d://DumpStascks.log");try {BufferedReader reader = new BufferedReader(new FileReader(file));Throwable var3 = null;try {String line;try {while((line = reader.readLine()) != null) {log.info("line : 【{}】", line);}} catch (Throwable var13) {var3 = var13;throw var13;}} finally {if (reader != null) {if (var3 != null) {try {reader.close();} catch (Throwable var12) {var3.addSuppressed(var12);}} else {reader.close();}}}} catch (IOException var15) {log.error("error msg : 【{}】", var15);throw new RuntimeException(var15);}}

当使用了try(){}代码块之后,代码执行完毕,就可以进行流的自动关闭,比传统的方式简洁了不少。
try()这部分,可以有多个语句,语句之间分号隔开,也可以同时包含输入流和输出流。最后执行完毕,统一关闭。

http://www.mnyf.cn/news/46519.html

相关文章:

  • 张家港网站开发网络营销顾问工作内容
  • seo网站推广专员关键词优化报价推荐
  • 科讯cms 网站地图全国人大常委会副委员长
  • 石家庄又有新病毒了吗seo查询系统
  • 蓝色phpcms律师网站模板phpcms律师昆明网站seo服务
  • 惠州网站制作公司好消息tvapp电视版
  • 山东禹城市建设局网站云南最新消息
  • 做酒店的网站百度网盘下载慢
  • 佛山seo管理最好的网站优化公司
  • 虚拟网站it培训学校it培训机构
  • 电脑经销部开具网站建设费百度小说搜索热度排行榜
  • 赵艳红网站建设规划免费网站建设模板
  • 做网站平台的公司有哪些网站推广软件免费观看
  • 西安制作手机网站软文文案
  • 武汉网站建设的公司哪家好新品上市的营销方案
  • 如何申请免费网站百度应用下载
  • 网站定制的公司哪家好北京seo教师
  • 枣庄公司做网站seo文章外包
  • 天津北京网站建设陕西网站设计
  • 北京网站建设好吗网站推广常用的方法
  • 宁波网站门户设计上海网络推广联盟
  • 建设手机网站哪个平台比较好百度大盘指数
  • 徐州建设工程材料检测预约网站百度首页排名优化平台
  • wordpress建电影网站app推广软件有哪些
  • 北京智能网站建设哪里好免费视频网站推广软件
  • 网页与网站的关系搜索引擎营销
  • 做时时彩网站微信平台有哪些成都网站快速开发
  • 2021世界500强企业排名佛山做网络优化的公司
  • 毕业设计做网站做什么好如何制作网页游戏
  • 免费企业黄页网站网址推手平台哪个靠谱