<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Java 9 - `try-with-resource`改進 > 原文: [https://beginnersbook.com/2018/05/java-9-try-with-resources-enhancements/](https://beginnersbook.com/2018/05/java-9-try-with-resources-enhancements/) **`try-with-resource`語句**首先在 Java 7 中引入。該語句在 [Java 9](https://beginnersbook.com/2018/04/java-9-features-with-examples/) 中得到了重大改進。在本指南中,我們將討論 Java 9 中`try-with-resource`語句的**改進**。 ## 什么是`try-with-resource`? 這個語句最初是在 Java 7 中引入的,以避免我們為異常處理編寫的冗余代碼。這句話的優點是: 1. 嘗試用資源自動關閉所有資源(文件,數據庫連接,網絡連接等)。無需明確關閉它們。這可以防止內存泄漏。 2. 借助`try-with-resource`,我們可以減少不必要的代碼行,使代碼更具可讀性。 ## 我們以前如何使用`try-with-resource`在 Java 7 中編寫代碼? 這就是我們在 Java 7 中使用`Try-With-Resource`語句的方式。 ```java import java.io.FileNotFoundException; import java.io.FileOutputStream; public class JavaExample { public static void main(String[] args) throws FileNotFoundException { try(FileOutputStream fileOutputStream = new FileOutputStream("beginnersbook.txt");){ //We are writing this string in the output file using FileOutputStream String mystring = "We are writing this line in the output file."; //Converting the given string in bytes byte bytes[] = mystring.getBytes(); //Writing the bytes into the file fileOutputStream.write(bytes); //Displaying success message after the successful write operation System.out.println("The given String is written in the file successfully"); }catch(Exception e) { System.out.println(e); } } } ``` 輸出: ```java The given String is written in the file successfully ``` ## Java 7 中的`try-with-resource`問題 Java 7 中的`Try-With-Resource`語句存在某些問題。此語句不允許在語句塊(范圍)之外聲明資源。讓我們舉一個例子來理解這一點。 **Java 7 - 在 Try-With-Resources 塊**之外聲明的資源 ```java import java.io.FileNotFoundException; import java.io.FileOutputStream; public class JavaExample { public static void main(String[] args) throws FileNotFoundException { FileOutputStream fileOutputStream = new FileOutputStream("beginnersbook.txt"); try(fileOutputStream){ String mystring = "We are writing this line in the output file."; byte bytes[] = mystring.getBytes(); fileOutputStream.write(bytes); System.out.println("The given String is written in the file successfully"); }catch(Exception e) { System.out.println(e); } } } ``` Java 7 中的輸出: ```java Compile-time error ``` 上面的示例拋出編譯時錯誤,因為資源是在`Try-With-Resource`語句的范圍之外聲明的。 **Java 7 - 外部聲明的資源 - 重復資源作為變通方法** 為了解決上述錯誤,我們不得不在 Java 7 中做一個解決方法。我們過去常常復制資源引用,如下所示: ```java import java.io.FileNotFoundException; import java.io.FileOutputStream; public class JavaExample { public static void main(String[] args) throws FileNotFoundException { FileOutputStream fileOutputStream = new FileOutputStream("beginnersbook.txt"); try(FileOutputStream fileOutputStream2 = fileOutputStream){ String mystring = "We are writing this line in the output file."; byte bytes[] = mystring.getBytes(); fileOutputStream2.write(bytes); System.out.println("The given String is written in the file successfully"); }catch(Exception e) { System.out.println(e); } } } ``` 這段代碼在 Java 7 中運行良好。 注意`try`塊中的`FileOutputStream fileOutputStream2 = fileOutputStream`行。我們在`Try-With-Resource`的范圍內創建了對已聲明的輸出流的另一個引用。 ## Java 9 - `try-with-resource`改進 **Java 9 為傳統的`Try-With-Resource`語句提供了一個重要的改進**。 Java 9 允許我們在 **`Try-With-Resource`塊**之外聲明資源。我們不再需要創建局部變量來訪問資源。讓我們采用與 Java 7 相同的示例,但遇到了編譯錯誤。在 Java 9 中,此代碼運行得非常好。 ```java import java.io.FileNotFoundException; import java.io.FileOutputStream; public class JavaExample { public static void main(String[] args) throws FileNotFoundException { FileOutputStream fileOutputStream = new FileOutputStream("beginnersbook.txt"); try(fileOutputStream){ String mystring = "We are writing this line in the output file."; byte bytes[] = mystring.getBytes(); fileOutputStream.write(bytes); System.out.println("The given String is written in the file successfully"); }catch(Exception e) { System.out.println(e); } } } ``` 輸出: ```java The given String is written in the file successfully ``` Eclipse Oxygen 運行 Java SE 9 中此代碼的屏幕截圖。 ![Java 9 - Try with resources enhancements](https://img.kancloud.cn/38/8d/388d0cc040da1b50192180ee4338b605_1024x499.jpg)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看