Use of File write stream FileWriter:
1 Import Staticorg.junit.assert.*;2 3 ImportJava.io.File;4 ImportJava.io.FileWriter;5 Importjava.io.IOException;6 7 Importorg.junit.Test;8 9 Ten One Public classMyTest { A //writes data to a specified file by a character stream - @Test - Public voidTestwrite () { theString Path = "F:" +file.separator+ "Demo.txt"; - //If the file does not exist, create the file first -File File =NewFile (path); - if(!file.exists ()) { + Try { - file.createnewfile (); +}Catch(IOException e) { A //TODO auto-generated Catch block at e.printstacktrace (); - } - } -FileWriter FileWriter =NULL; - Try { - //in Windows, make sure that files with path paths are present inFileWriter =NewFileWriter (path); -Filewriter.write ("You aren't a good boy!"); to Filewriter.flush (); + filewriter.close (); -}Catch(IOException e) { the //TODO auto-generated Catch block * e.printstacktrace (); $}finally{Panax Notoginseng //If you throw an exception, it means that the FileWriter object is not created. - if(filewriter!=NULL){ the Try { + filewriter.close (); A}Catch(IOException e) { the //TODO auto-generated Catch block + e.printstacktrace (); - } $ } $ - } - the - }Wuyi the}
... One of the learning of IO streams ...