Conversion stream-outputstreamwriter class and inputstreamreader class

Source: Internet
Author: User

12.4 conversion stream -- outputstreamwriter class and inputstreamreader class

The entire Io package is actually divided into byte streams and byte streams, but in addition to these two streams, there is also a set of byte streams-the conversion class of the byte streams.

Outputstreamwriter: A child of writer. It converts the output bytes stream into a byte stream, that is, the output object of a bytes stream into a byte stream output object.

Inputstreamreader: a sub-class of reader. It converts the input byte stream into a bytes stream, that is, the input object of a byte stream into the input object of the bytes stream.

Taking file operations as an example, the character data in the memory needs to be converted into a byte stream through outputstreamwriter to be saved in the file. During reading, the byte that is read must flow through inputstreamreader into a bytes stream, the conversion steps are 12-7.

 
(Click to view the larger image) Figure 12-7 conversion steps

From Figure 12-7, we can clearly find that all operations are finally saved in the file in bytes.

The outputstreamwriter construction method is as follows:

 
 
  1. Public outputstreamwriter (outputstream out)

Example: convert a byte output stream to a character output stream

 
 
  1. Package org. lxh. iodemo. changeiodemo;
  2. Import java. Io. file;
  3. Import java. Io. fileoutputstream;
  4. Import java. Io. outputstreamwriter;
  5. Import java. Io. Writer;
  6. Public class outputstreamwriterdemo01 {
  7. Public static void main (string [] ARGs)
    Throws exception {// All exceptions
  8. Throw
  9. File F = new file ("D:" + file. Separator + "test.txt ");
  10. Writer out = NULL;
  11. Out = new outputstreamwriter (New fileoutputstream (f); // The byte stream is converted to the bytes stream.
  12. Out. Write ("Hello World"); // use the outputs stream to output
  13. Out. Close ();
  14. }
  15. }

Program Execution result:

 

Example: convert a byte input stream to a character input stream
 
 
  1. Package org. lxh. iodemo. changeiodemo;
  2. Import java. Io. file;
  3. Import java. Io. fileinputstream;
  4. Import java. Io. inputstreamreader;
  5. Import java. Io. reader;
  6. Public class inputstreamreaderdemo01 {
  7. Public static void main (string [] ARGs) throws
    Exception {// All exceptions are thrown
  8. File F = new file ("D:" + file. Separator + "test.txt ");
  9. Reader reader = NULL;
  10. Reader = new inputstreamreader (New fileinputstream (f); // converts a byte stream to a bytes stream.
  11. Char [] C = new char [1024];
  12. Int Len = reader. Read (C );
  13. Reader. Close ();
  14. System. Out. println (new string (C, 0, Len ));
  15. }
  16. }

 

U prompt: Description of filewriter and filereader.

From the JDK documentation, we can know that fileoutputstream is the direct subclass of outputstream, and fileinputstream is also the direct subclass of inputstream. However, there are some special operations in the two operation classes in the ghost stream file, filewriter is not a subclass of writer, but a subclass of outputstreamwriter. filereader is not a subclass of reader, but a subclass of inputstreamreader, from the inheritance relationships between these two classes, we can clearly find that both byte streams and batch streams are actually operating the input/output streams in bytes.

Conversion stream-outputstreamwriter class and inputstreamreader class

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.