Java Notes #02 # Try statement with resource

Source: Internet
Author: User

Index
    1. Ordinary Try.java.
    2. Try.java with resources
    3. When the resource is null
    4. Documents and materials that can be consulted

/

Test.txt

What you want to read

Hello.

/

Ordinary Try.java.

Read Test.txt content

 Packageexample;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException; Public classGeneraltry { Public Static voidMain (string[] args) {FileInputStream InputStream=NULL; Try{InputStream=NewFileInputStream ("D:/labs/test.txt"); System.out.println ((Char) Inputstream.read ());//The output reads to the first character, as to whether the TXT text content corresponds to the TXT itself is also related to the character encoding. }Catch(FileNotFoundException e) {//The catch exception is mandatory, corresponding to the new FileInputStream ...E.printstacktrace (); } Catch(IOException e) {//The catch exception is mandatory, corresponding to the inputstream.read.E.printstacktrace (); } finally{//shutting down resources is not mandatory, but we should always do that .            Try{inputstream.close (); if(InputStream! =NULL) { ///  InputStream may be empty, in order to prevent the occurrence of NULL pointers and cause program GG  .Inputstream.close (); }            } Catch(IOException e) {e.printstacktrace (); }} System.out.println ("If you see this sentence in the Output window, the program does not have GG"); }}/*Output=h If you see this sentence in the Output window, the program does not have GG*/

/

Try.java with resources

Read Test.txt content, too

 Packageexample;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException; Public classResourcetry { Public Static voidMain (string[] args) {Try(FileInputStream InputStream =NewFileInputStream ("D:/labs/test.txt") {System.out.println (Char) Inputstream.read ());//The output reads to the first character, as to whether the TXT text content corresponds to the TXT itself is also related to the character encoding. }Catch(FileNotFoundException e) {//catch exception is still mandatoryE.printstacktrace (); } Catch(IOException e) {//catch exception is still mandatoryE.printstacktrace (); }    inputstream.close () is automatically called when the try block exits System.out.println ("If you see this statement in the Output window, the program does not have GG"); }}/*Output=h If you see this sentence in the Output window, the program does not have GG*/

/

The above program (a try program with resources) is run under normal circumstances (test.txt file exists), so if Test.txt does not exist? Try to change the test.txt to a non-existent test2.txt the output result is as follows:

/* output= If you see this sentence in the Output window, the program does not have Ggjava.io.filenotfoundexception:d:\labs\test2.txt (the system cannot find the file specified.) ) at    Java.io.FileInputStream.open0 (Native Method) at    Java.io.FileInputStream.open (Fileinputstream.java : 195) at    java.io.fileinputstream.<init> (fileinputstream.java:138) at    java.io.fileinputstream.< Init> (fileinputstream.java:93) at    example. Resourcetry.main (resourcetry.java:10)* /

If the first program (normal try) does not have a non-null check before Inputstream.close (), the program will abort because of java.lang.NullPointerException (that is, GG).

As seen above, the try test program with resources can also execute gracefully, so a try with a resource has a non-null judgment before calling Close (), which ensures that the program executes properly without throwing nullpointerexception, and it is important to note that Except for null pointer exceptions, the other exceptions thrown by close () need to be a different story!

/

Documents and materials that can be consulted:

Try-with Resource when autocloseable is null

Possible null pointer exception on autocloseable idiom

The Try-with-resources Statement-java tutorials-oracle

Java language specification-14.20.3

Oracle Blog-project coin:try-with-resources on a null resource

Java Notes #02 # Try statement with resource

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.