ITPub博客

首页 > > > 连环清洁工之特殊任务--java资源如何关闭?

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

作者:zhmw 时间:2019-12-27 10:23:11 0 删除 编辑

小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:学无止境,一起加油!今天到这里了,我还要继续我的游戏呢。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69901620/viewspace-3161004/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2018-12-04

  • 博文量
    295
  • 访问量
    9531