We want to write something in a file, the initial version:
Import java.io.*;class filewriterdemo{public static void Main (string[] args) {FileWriter fw = new FileWriter (" Demo.txt ");//Create stream, open (if not present, new) file Fw.write (" ABCDEFG ");//write Content fw.close ();/close stream (flush () stream before closing)}}
But the above three sentences will be abnormal--
The first sentence:
For example, it is written as FileWriter fw = new FileWriter ("Ttt:\\demo.txt"), which is obviously wrong, because there is no TTT for this drive letter!
The second sentence:
Write content is too large, the entire computer hard drive can not fit, there will be an exception.
The third sentence:
If the stream is not successfully created at all, it is not closed. So there will be exceptions.
So we are going to try...catch ... Processing:
Import java.io.*;class filewriterdemo{public static void Main (string[] args) {try{filewriter fw = new FileWriter (" Demo.txt "); Fw.write (" ABCDEFG "); Fw.close ();} catch (IOException e) {System.out.println ("catch:" +e.tostring ())}}}
It's still wrong to deal with it! For example, the second sentence is abnormal, then handle the exception, then Fw.close () will not be executed, that is, the resource is not closed, so fw.close () this sentence whether there is no exception to execute, to put it into the try...catch...finally ... In the Finally:
Class filewriterdemo{public static void Main (string[] args) {try{filewriter fw = new FileWriter ("Demo.txt"); Fw.write ("ABCDEFG");} catch (IOException e) {System.out.println ("catch:" +e.tostring ());} Finally{fw.close ();}}}
This will compile the error, will say Fw.close () FW can not be found, because the FW is created in a try, and finally belongs to different blocks of code, so to declare the FW out:
Class filewriterdemo{public static void Main (string[] args) {FileWriter fw = null;//Outer declaration TRY{FW = new FileWriter ("Demo . txt ");//Inside Create Fw.write (" ABCDEFG ");} catch (IOException e) {System.out.println ("catch:" +e.tostring ());} Finally{fw.close ();}}}
The FW was finally put to the end. Is that always OK? No way!
Put Fw.close () into the finally inside, the equivalent of this sentence will be executed, but the exception of this sentence is not handled Ah! Because we took this sentence out of the try to handle the exception, so we have to re-try...catch:
Class filewriterdemo{public static void Main (string[] args) {FileWriter fw = null;//Outer declaration TRY{FW = new FileWriter ("Demo . txt ");//Inside Create Fw.write (" ABCDEFG ");} catch (IOException e) {System.out.println ("catch:" +e.tostring ());} finally{try{//handle Fw.close () in Finally, try again fw.close ();} catch (IOException e) {System.out.println (e.tostring ());}}}}
Finally compiled through!!!
However, there is still an inappropriate place-
For example, the first sentence is written FileWriter fw = new FileWriter ("Ttt:\\demo.txt"), not only the first try block will report an exception, the second try Block (Fw.close () The try block is also reported abnormal--java.lang.nullpointerexception, because the FW creation is not successful, naturally not closed, so it will be caught exception, so it is better to judge before closing, that is:
if (fw!=null)
Fw.close ();
The finally block is written like this:
Finally{try{if (Fw!=null) Fw.close ();} catch (IOException e) {System.out.println (e.tostring ());}}
Or something like this:
Finally{if (Fw!=null)//First determine whether the empty try{fw.close ();} catch (IOException e) {System.out.println (e.tostring ());}}
Finally OK:
Class filewriterdemo{public static void Main (string[] args) {FileWriter fw = null;//Outer declaration TRY{FW = new FileWriter ("Demo . txt ");//Inside Create Fw.write (" ABCDEFG ");} catch (IOException e) {System.out.println ("catch:" +e.tostring ());} finally{try{//handles the Fw.close () exception in Finally, try again if (fw!=null) Fw.close ();} catch (IOException e) {System.out.println (e.tostring ());}}}}
How Java IO exceptions are handled