Stream is for byte [] operations and binary file operations.
Reader and writer are used for char operations and text file operations. By default, the native encoding method is used to write char, such as UTF-8 and UTF-16 and gb2312 encoding.
Reader and writer are abstract classes and cannot be used directly. Generally, filereader and filewriter are used.
If the code is specific, you must use outputstreamwriter. Note that the outputstreamwriter constructor requires an outputstream instance.
Bufferedreader andBufferedwriter
Optimizes common reader and writer.
Printwriter
In addition, printwriter supports a series of print functions and different encoding methods.
Like printwriter, printstream provides a series of print functions. For example, system. Out is printstream.
Encoding Method
Both outputstreamwriter and printwriter support different encoding methods.
Filewriter only supports the default encoding method.
Import Java. Io. fileoutputstream; Import Java. Io. filewriter; Import Java. Io. ioexception; Import Java. Io. outputstreamwriter; Import Java. Io. printwriter; Import Java. util. arrays; Public Class Mytest { Public Static Void Test1 () Throws Ioexception {fileoutputstream Fos = New Fileoutputstream (" C: \ test. dat "); Byte [] Buf = New Byte [1, 1024]; arrays. Fill (BUF ,( Byte ) 'A'); FOS. Write (BUF); FOS. Close ();} Public Static Void Test2 () Throws Ioexception {filewriter writer = New Filewriter (" C: \ test.txt "); Char C = 'a'; string temp =" Test "; Writer. Write (c); writer. Write (temp); writer. Close ();} Public Static Void Test3 () Throws Ioexception {outputstreamwriter writer = New Outputstreamwriter ( New Fileoutputstream (" C: \ test.txt ")," UTF-8 "); Writer. append (" Beijing Institute of Technology "); Writer. Close ();} Public Static Void Test4 () Throws Ioexception {printwriter writer = New Printwriter ("C: \ 1.txt "," UTF-16 "); Writer. append (" Beijing Institute of Technology "); Writer. Close ();} Public Static Void Main (string [] ARGs) Throws Ioexception {test4 (); system. Out. println (" OK ");}} |