Stream vs reader writer

Source: Internet
Author: User

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 andBufferedwriterOptimizes 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 ");}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.