日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2025/4/5 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 连环清洁工之特殊任务--java资源如何关闭? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

小C是一名特殊的黑客,他專門為黑客提供服務(wù),掃除黑客攻擊的痕跡,避免被查到為何人攻擊。

?

今天他正興致勃勃的玩游戲《連環(huán)清潔工》,連環(huán)清潔工是由iFun4all S.A.制作發(fā)行的一款犯罪題材動作冒險類游戲,故事劇情講述的是一個專門山寨別人的殺手,專門模仿最近發(fā)生的大案要案,制造類似的兇殺案。游戲中玩家扮演一名專業(yè)兇案現(xiàn)場清掃人員,為客戶處理尸體、清理血跡、隱藏兇器等犯罪證據(jù),玩家接受任務(wù)的時候不能問任何問題。

突然接到小白請求幫助的緊急電話,注:小白是小C入侵小白的電腦后認識的,詳情太多,參見詳細地址https://cloud.tencent.com/developer/news/333203。

原來小白在學習java,碰到一個編程問題:文件操作關(guān)閉資源的時候會莫名其妙的報錯。代碼如下:

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針對小白剛剛編程的經(jīng)歷,采用循循誘導的方式。

小C:上面的代碼是不是沒有捕獲異常?是不是可以把異常捕獲到,再分析異常原因?

小白:對哦,那我使用try 。。catch試試

public void openFile(){try {// constructor may throw FileNotFoundExceptionFileReader reader = new FileReader("someFile");int i=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:做的很不錯,知道捕捉多重異常了!,資源的關(guān)閉是不是放到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的關(guān)閉,是不是還有可能拋出異常,是不是還要捕獲?

小白:是哦,我忘記了,修改后的是這樣的嗎?

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()內(nèi)部, JVM會調(diào)用java.lang.AutoCloseable.close() 方法,自動關(guān)閉try()內(nèi)部的資源。

小白:厲害,我學會了。

小C:那我考考你。

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

這個會打印出什么結(jié)果?

小白:“hello world” 因為return 退出了,finally不能執(zhí)行。

小C:不對,finally總是會執(zhí)行的,打印

Hello world

Goodbye world

小白:我明白了,finally總是會執(zhí)行的。

小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語句塊的執(zhí)行是正常地還是意外地結(jié)束,finally語句塊確實都會執(zhí)行。

然而在這個程序中,try 語句塊根本就沒有結(jié)束其執(zhí)行過程。System.exit 方法

將停止當前線程和所有其他當場死亡的線程。finally 子句的出現(xiàn)并不能給予線

程繼續(xù)去執(zhí)行的特殊權(quán)限。

如果想要執(zhí)行,想要使用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:學無止境,一起加油!今天到這里了,我還要繼續(xù)我的游戲呢。

參考資料

【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解惑

轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/p/11566466.html

總結(jié)

以上是生活随笔為你收集整理的连环清洁工之特殊任务--java资源如何关闭?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。