Index
- Ordinary Try.java.
- Try.java with resources
- When the resource is null
- 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