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:
- Public outputstreamwriter (outputstream out)
Example: convert a byte output stream to a character output stream
- Package org. lxh. iodemo. changeiodemo;
- Import java. Io. file;
- Import java. Io. fileoutputstream;
- Import java. Io. outputstreamwriter;
- Import java. Io. Writer;
- Public class outputstreamwriterdemo01 {
- Public static void main (string [] ARGs)
Throws exception {// All exceptions
- Throw
- File F = new file ("D:" + file. Separator + "test.txt ");
- Writer out = NULL;
- Out = new outputstreamwriter (New fileoutputstream (f); // The byte stream is converted to the bytes stream.
- Out. Write ("Hello World"); // use the outputs stream to output
- Out. Close ();
- }
- }
Program Execution result:
Example: convert a byte input stream to a character input stream
- Package org. lxh. iodemo. changeiodemo;
- Import java. Io. file;
- Import java. Io. fileinputstream;
- Import java. Io. inputstreamreader;
- Import java. Io. reader;
- Public class inputstreamreaderdemo01 {
- Public static void main (string [] ARGs) throws
Exception {// All exceptions are thrown
- File F = new file ("D:" + file. Separator + "test.txt ");
- Reader reader = NULL;
- Reader = new inputstreamreader (New fileinputstream (f); // converts a byte stream to a bytes stream.
- Char [] C = new char [1024];
- Int Len = reader. Read (C );
- Reader. Close ();
- System. Out. println (new string (C, 0, Len ));
- }
- }
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