绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
连环清洁工之特殊任务--java资源如何关闭?
2019-12-27 10:23:11

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

 

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

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

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

publicvoidopenFile()throws IOException {

FileReader reader =newFileReader("someFile");

inti=0;

while(i != -1){

i = reader.read();

System.out.println((char) i );

}

reader.close();

System.out.println("--- File End ---");

}

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

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

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

publicvoid openFile(){

try {

// constructor may throw FileNotFoundExceptionFileReader reader =newFileReader("someFile");

inti=0;

while(i != -1){

//reader.read() may throw IOExceptioni = 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比较好?

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

publicvoidopenFile()throws IOException {

FileReader reader =null;

try {

reader =newFileReader("someFile");

inti=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的判断,再改一下:

publicvoidopenFile()throws IOException {

FileReader reader =null;

try {

reader =newFileReader("someFile");

inti=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的关闭,是不是还有可能抛出异常,是不是还要捕获?

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

publicvoidopenFile()throws IOException {

FileReader reader =null;

try {

reader =newFileReader("someFile");

inti=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:那你看看这个是否简洁了一些呢?

publicvoidopenFile()throws IOException {

try(FileReader reader =newFileReader("someFile")){

;

inti=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:那我考考你。

publicstaticvoid 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:那可不一定哦,看看这个:

publicstaticvoid 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

publicstaticvoid main(String[] args) {

System.out.println("Hello world");

Runtime.getRuntime().addShutdownHook(

new Thread() {

publicvoid run() {

System.out.println("Goodbye world");

}

});

System.exit(0);

}

小白:好神奇!

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

分享好友

分享这个小栈给你的朋友们,一起进步吧。

JAVA玩具小屋
创建时间:2019-08-16 16:54:49
分享程序开发方面的小经验,思考一些比较简单易懂的技术问题
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

栈主、嘉宾

查看更多
  • Yios5092
    栈主

小栈成员

查看更多
  • 栈栈
  • coyan
  • 25minutes
  • ?
戳我,来吐槽~