A write file is similar to a read file, which can be written in bytes and can be written in a character unit.
The corresponding read operation FileOutputStream is written in bytes:
1FileOutputStream fileoutputstream=NULL;2String astring= "Hello";3 Try {4fileoutputstream=NewFileOutputStream (afile);5 //fileoutputstream=new FileOutputStream ("E:/a.txt");6 //fileoutputstream=new FileOutputStream (afile,true);7 Fileoutputstream.write (Astring.getbytes ()); 8}Catch(FileNotFoundException e) {9 //TODO auto-generated Catch blockTen e.printstacktrace (); One}Catch(IOException e) { A //TODO auto-generated Catch block - e.printstacktrace (); -}finally { the if(fileoutputstream!=NULL) - { - Try { -Fileoutputstream.flush ();//The write method does not output data immediately to the specified destination, is staged in memory, and flush () indicates that the data in the buffer is forced to be sent out + fileoutputstream.close (); -}Catch(IOException e) { + //TODO auto-generated Catch block A e.printstacktrace (); at } - } -}
Here is just a simple replacement, if you want to implement the additional words only need to write FileOutputStream (afile,true); write the second argument as true.
The Filewriter,bufferedwriter is written in characters and can be written directly to the string
1FileWriter filewriter=NULL;2 Try {3Filewriter=NewFileWriter (afile);4 //FileWriter filewriter=new FileWriter ("");5 //FileWriter filewriter=new FileWriter (afile,true);//file Continuation, append6 Filewriter.write (astring); 7}Catch(IOException e) {8 //TODO auto-generated Catch block9 e.printstacktrace ();Ten}finally { One if(filewriter!=NULL) A { - Try { - Filewriter.flush (); the filewriter.close (); -}Catch(IOException e) { - //TODO auto-generated Catch block - e.printstacktrace (); + } - } +}
Writing to Java files