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

在哪里找人做网站靠谱桔子seo

在哪里找人做网站靠谱,桔子seo,桂平做网站公司,淘宝的网站建设文章目录 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.hkea.cn/news/146125/

相关文章:

  • 设置本机外网ip做网站网站免费制作平台
  • 有什么推荐做简历的网站2024的新闻有哪些
  • 申请做网站 论坛版主惠州seo外包服务
  • 网站照片上传不了域名解析ip
  • 胖小七网站建设2022最新国际新闻10条简短
  • wordpress 网站备份厦门seo外包服务
  • 网站建设及推广培训杭州百度快照优化排名
  • 简单手机网站开发软件关键词排名代发
  • visio画网站开发类图注册域名后怎么建网站
  • 道里网站运营培训北京网络营销咨询公司
  • 目前做网站流行的语言seo关键词排名优化哪家好
  • 长沙营销型网站制作费用seo图片优化
  • 学生诚信档案建设网站seo数据分析
  • 北京住房城乡建设厅网站首页1688官网入口
  • 网站建设需要懂什么软件徐州百度seo排名优化
  • wordpress网站样式网站排名查询
  • 郑州网站建设推销外贸网站推广与优化
  • 当当网站开发系统说明搜索引擎排名google
  • 国外男女直接做的视频网站企业邮箱登录入口
  • 成都可以做网站的公司百度手机助手最新版下载
  • 赤峰网站建设招聘市场营销互联网营销
  • 网站开发后端需要哪些技术友情链接检索数据分析
  • 金华竞价排名 金华企业网站建设常见的网络营销平台有哪些
  • p2p网站开发关键词seo是什么意思
  • 自己免费怎么制作网站合肥今天的最新消息
  • 今日头条新闻10条简短seo网络优化招聘信息
  • 赣州人才网官方网站关键词seo优化软件
  • cad做兼职区哪个网站郑州网络营销公司排名
  • 宁夏银川做网站的公司有哪些网络营销分类
  • 换物网站为什么做不起来中国免费广告网