连环清洁工之特殊任务–java资源如何关闭?

  • 2019 年 10 月 3 日
  • 笔记

小C是一名特殊的黑客,他专门为黑客提供服务,扫除黑客攻击的痕迹,避免被查到为何人攻击。

java清道夫的特殊任务

 

今天他正兴致勃勃的玩游戏《连环清洁工》,连环清洁工是由iFun4all S.A.制作发行的一款犯罪题材动作冒险类游戏,故事剧情讲述的是一个专门山寨别人的杀手,专门模仿最近发生的大案要案,制造类似的凶杀案。游戏中玩家扮演一名专业凶案现场清扫人员,为客户处理尸体、清理血迹、隐藏凶器等犯罪证据,玩家接受任务的时候不能问任何问题。

突然接到小白请求帮助的紧急电话,注:小白是小C入侵小白的电脑后认识的,详情太多,参见详细地址https://cloud.tencent.com/developer/news/333203。

原来小白在学习java,碰到一个编程问题:文件操作关闭资源的时候会莫名其妙的报错。代码如下:

 public void openFile() throws IOException {   FileReader reader = new FileReader("someFile");   int i=0;   while(i != -1){   i = reader.read();   System.out.println((char) i );   }   reader.close();   System.out.println("--- File End ---");   }

小C针对小白刚刚编程的经历,采用循循诱导的方式。

小C:上面的代码是不是没有捕获异常?是不是可以把异常捕获到,再分析异常原因?

小白:对哦,那我使用try 。。catch试试

 public void openFile(){   try {   // constructor may throw FileNotFoundException   FileReader reader = new FileReader("someFile");   int i=0;   while(i != -1){   //reader.read() may throw IOException   i = reader.read();   System.out.println((char) i );   }   reader.close();   System.out.println("--- File End ---");   } catch (FileNotFoundException e) {   //do something clever with the exception   } catch (IOException e) {   //do something clever with the exception   }   }

小C:做的很不错,知道捕捉多重异常了!,资源的关闭是不是放到finally比较好?

小白:对哦,我看语法有这样的,那我重新写一下

    public void openFile() throws IOException {          FileReader reader = null;          try {              reader = new FileReader("someFile");              int i=0;              while(i != -1){                  i = reader.read();                  System.out.println((char) i );              }          }  catch (FileNotFoundException e) {              //do something clever with the exception          } catch (IOException e) {              //do something clever with the exception          }finally {              reader.close();              System.out.println("--- File End ---");          }      }

小白:哦,还忘掉reader的判断,再改一下:

    public void openFile() throws IOException {          FileReader reader = null;          try {              reader = new FileReader("someFile");              int i=0;              while(i != -1){                  i = reader.read();                  System.out.println((char) i );              }          }  catch (FileNotFoundException e) {              //do something clever with the exception          } catch (IOException e) {              //do something clever with the exception          }finally {              if(reader != null){                  reader.close();              }              reader.close();              System.out.println("--- File End ---");          }      }

 

小C:reader的关闭,是不是还有可能抛出异常,是不是还要捕获?

小白:是哦,我忘记了,修改后的是这样的吗?

    public void openFile() throws IOException {          FileReader reader = null;          try {              reader = new FileReader("someFile");              int i=0;              while(i != -1){                  i = reader.read();                  System.out.println((char) i );              }          }  catch (FileNotFoundException e) {              //do something clever with the exception          } catch (IOException e) {              //do something clever with the exception          }finally {              if(reader != null){                  try {                      reader.close();                  } catch (IOException e) {                      //do something clever with the exception                  }              }              reader.close();              System.out.println("--- File End ---");          }      }

小C:代码是不是太繁琐了?有没有更简洁的办法?让jvm帮你处理一些繁琐的工作?

小白:听说过ry-with-resources,但没有用过。

小C:那你看看这个是否简洁了一些呢?

    public void openFile() throws IOException {          try (FileReader reader = new FileReader("someFile")){              ;              int i=0;              while(i != -1){                  i = reader.read();                  System.out.println((char) i );              }          }  catch (FileNotFoundException e) {              //do something clever with the exception          } catch (IOException e) {              //do something clever with the exception          }      }

把资源放到try()内部, JVM会调用java.lang.AutoCloseable.close() 方法,自动关闭try()内部的资源。

小白:厉害,我学会了。

小C:那我考考你。

    public static void main(String[] args) {          try {              System.out.println("Hello world");              return;              } finally {              System.out.println("Goodbye world");              }      }      

这个会打印出什么结果?

小白:“hello world” 因为return 退出了,finally不能执行。

小C:不对,finally总是会执行的,打印

Hello world

Goodbye world

小白:我明白了,finally总是会执行的。

小C:那可不一定哦,看看这个:

    public static void main(String[] args) {          try {              System.out.println("Hello world");              System.exit(0);              } finally {              System.out.println("Goodbye world");              }      }

小白:不是打印?

Hello world

Goodbye world

小C:不论try语句块的执行是正常地还是意外地结束,finally语句块确实都会执行。

然而在这个程序中,try 语句块根本就没有结束其执行过程。System.exit 方法

将停止当前线程和所有其他当场死亡的线程。finally 子句的出现并不能给予线

程继续去执行的特殊权限。

如果想要执行,想要使用hook

    public static void main(String[] args) {          System.out.println("Hello world");          Runtime.getRuntime().addShutdownHook(          new Thread() {          public void run() {          System.out.println("Goodbye world");          }          });          System.exit(0);          }

小白:好神奇!

小C:学无止境,一起加油!今天到这里了,我还要继续我的游戏呢。

参考资料

【1】http://tutorials.jenkov.com/java-exception-handling/basic-try-catch-finally.html

【2】https://howtodoinjava.com/java/exception-handling/try-catch-finally/

【3】https://howtodoinjava.com/java7/try-with-resources/

【4】java解惑