[Java iO] _ byte-character conversion stream notes
Objectives of this chapter:
Measure the test taker's knowledge about the functions of outputstreamwriter and inputstreamreader.
Outputstreamwriter and intputstreamreader
Inputstream and outputstream are established with reader and writer because of outputstreamwriter and inputstreamreader.
Therefore, this chapter is more important than the previous chapter.
In the entire Io package, it is actually a byte stream and a batch stream, but in addition to these two streams, there is also a set of byte stream-Batch stream conversion classes.
Outputstreamwriter: A Child class of writer. It converts the output bytes stream into a byte stream, that is, it changes the output object of a bytes stream into the output object of the byte stream.
Inputstreamreader: A Child class of reader. It converts the input byte stream into a bytes stream, that is, it changes the input object of a byte stream into the input object of the bytes stream.
Generally, you need to use byte or dense stream when operating the input and output content, but sometimes you need to change the dense stream to the byte stream form or the byte to the dense stream form. Therefore, you need another set of Operation classes for the conversion stream.
Conversion steps:
If the file operation is used as an example, character data in the memory needs to be converted into a byte stream through outputstreamwriter before it can be saved in the file. During reading, the byte that is read must flow through inputstreamreader to a bytes stream.
In the outputstreamwriter class, an object of the byte stream is required:
Public outputstreamwriter (outputstream out)
For example, output a byte file stream as a character.
Import Java. io. *; public class outputstreamwriterdemo01 {public static void main (string ARGs []) throws exception {file F = new file ("D:" + file. separator + "test.txt"); writer out = NULL; out = new outputstreamwriter (New fileoutputstream (f); out. write ("Hello world !!! "); Out. Close ();}}
When reading, you can also use the byte stream to read the file of the byte stream.
Import Java. io. *; public class inputstreamreaderdemo01 {public static void main (string ARGs []) throws exception {file F = new file ("D:" + file. separator + "test.txt"); reader = NULL; reader = new inputstreamreader (New fileinputstream (f); char C [] = new char [1024]; int Len = reader. read (c); system. out. println (new string (C, 0, Len ));}}
However, the above is just an example of file operations, because outputstreamwriter receives the type of outputstream, as long as it is a byte output stream can be operated in the form of characters, and inputstreamreader () the received type is inputstream. As long as it is a byte input stream, you can use character input stream operations.
Sort the class relationships of byte streams:
Java. Lang. Object
Java. Io. Writer
Java. Io. outputstreamwriter
Java. Io. filewriter
Java. Lang. Object
Java. Io. Reader
Java. Io. inputstreamreader
Java. Io. filereader