1, the output stream of three ways
1) output by character, overwrite the original content
2) output by Byte, overwrite the original content
3) do not overwrite the original content
Package Output;import Java.io.bufferedwriter;import Java.io.file;import java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.filewriter;import Java.io.ioexception;import Java.io.OutputStream;import Java.io.printwriter;public class Outputtest {public static void main (string[] args) {File file = new file ("C:\\users\\admi Nistrator\\desktop\\zzz.txt ");//Characterwriter (file," abc ");//bytewriter (file," abc "); CharacterWriter2 (file, "123");} /* * 1, output by character, the contents of the output directly overwrite the previous text */public static void Characterwriter (file file, String count) {BufferedWriter BufferedWriter = null;try {bufferedwriter = new BufferedWriter (new PrintWriter (file)); Bufferedwriter.write (count); Bufferedwriter.flush ();} catch (IOException e) {e.printstacktrace ();} finally {if (bufferedwriter! = null) {try {bufferedwriter.close ();} catch (IO Exception e) {e.printstacktrace ();}}}} /* * 2, output by byte, overwrite original content */public static void Bytewriter (file file, String count) {OutputStream OutputStream = null;try {output Stream =New FileOutputStream (file); Outputstream.write (Count.getbytes ()); Outputstream.flush ();} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} finally {if ( OutputStream = null) {try {outputstream.close ();} catch (IOException e) {e.printstacktrace ();}}}} /* * 3, the content of the output does not overwrite the previous content */public static void CharacterWriter2 (file file, String count) {BufferedWriter bufferedwriter = null; try {bufferedwriter = new BufferedWriter (new FileWriter (file, True)); Bufferedwriter.write (count); Bufferedwriter.flush ();} catch (IOException e) {e.printstacktrace ();} finally {if (bufferedwriter! = null) {try {bufferedwriter.close ();} catch (IO Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}}
Output streams in Java