连环清洁工之特殊任务--java资源如何关闭?
小C是一名特殊的黑客,他專門為黑客提供服務(wù),掃除黑客攻擊的痕跡,避免被查到為何人攻擊。
?
今天他正興致勃勃的玩游戲《連環(huán)清潔工》,連環(huán)清潔工是由iFun4all S.A.制作發(fā)行的一款犯罪題材動(dòng)作冒險(xiǎn)類游戲,故事劇情講述的是一個(gè)專門山寨別人的殺手,專門模仿最近發(fā)生的大案要案,制造類似的兇殺案。游戲中玩家扮演一名專業(yè)兇案現(xiàn)場(chǎng)清掃人員,為客戶處理尸體、清理血跡、隱藏兇器等犯罪證據(jù),玩家接受任務(wù)的時(shí)候不能問任何問題。
突然接到小白請(qǐng)求幫助的緊急電話,注:小白是小C入侵小白的電腦后認(rèn)識(shí)的,詳情太多,參見詳細(xì)地址https://cloud.tencent.com/developer/news/333203。
原來小白在學(xué)習(xí)java,碰到一個(gè)編程問題:文件操作關(guān)閉資源的時(shí)候會(huì)莫名其妙的報(bào)錯(cuò)。代碼如下:
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針對(duì)小白剛剛編程的經(jīng)歷,采用循循誘導(dǎo)的方式。
小C:上面的代碼是不是沒有捕獲異常?是不是可以把異常捕獲到,再分析異常原因?
小白:對(duì)哦,那我使用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:做的很不錯(cuò),知道捕捉多重異常了!,資源的關(guān)閉是不是放到finally比較好?
小白:對(duì)哦,我看語法有這樣的,那我重新寫一下
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:代碼是不是太繁瑣了?有沒有更簡(jiǎn)潔的辦法?讓jvm幫你處理一些繁瑣的工作?
小白:聽說過ry-with-resources,但沒有用過。
小C:那你看看這個(gè)是否簡(jiǎn)潔了一些呢?
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會(huì)調(diào)用java.lang.AutoCloseable.close() 方法,自動(dòng)關(guān)閉try()內(nèi)部的資源。
小白:厲害,我學(xué)會(huì)了。
小C:那我考考你。
public static void main(String[] args) {try {System.out.println("Hello world");return;} finally {System.out.println("Goodbye world");}}這個(gè)會(huì)打印出什么結(jié)果?
小白:“hello world” 因?yàn)閞eturn 退出了,finally不能執(zhí)行。
小C:不對(duì),finally總是會(huì)執(zhí)行的,打印
Hello world
Goodbye world
小白:我明白了,finally總是會(huì)執(zhí)行的。
小C:那可不一定哦,看看這個(gè):
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語句塊確實(shí)都會(huì)執(zhí)行。
然而在這個(gè)程序中,try 語句塊根本就沒有結(jié)束其執(zhí)行過程。System.exit 方法
將停止當(dāng)前線程和所有其他當(dāng)場(chǎng)死亡的線程。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:學(xué)無止境,一起加油!今天到這里了,我還要繼續(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资源如何关闭?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 消灭 Java 代码的“坏味道”【转】
- 下一篇: Markdown基本语法【转】