Document directory
- OutputStreamWriter and InputStreamReader
OutputStreamWriter and InputStreamReader
In the entire IO package, it is actually divided into byte streams and bytes streams, but in addition to these two streams, there is also a set of byte streams-the conversion class of bytes streams.
OutputStreamWriter: a sub-class of Writer. It converts the output bytes stream into a byte stream, that is, it converts the output object of a bytes stream into the output object of the byte stream.
InputStreamReader: a sub-class of Reader that converts the input byte stream into a bytes stream, that is, converting the input object of a byte stream into the input object of the bytes stream.
Generally, you need to use byte or byte stream to operate the input and output content, but sometimes you need to convert the byte stream into the byte stream form or the byte stream into the byte stream form. Therefore, you need another set of Operation classes for the conversion stream.
Let's take a look at the OutputStreamWriter construction method:
Public OutputStreamWriter (OutputStream out)
For example, output a byte file stream as a character
1 import java.io.File;
2 import java.io.FileOutputStream;
3 import java.io.IOException;
4 import java.io.OutputStreamWriter;
5 import java.io.Writer;
6
7 public class Test20 {
8 public static void main(String[] args) throws IOException {
9 File f = new File("d:" + File.separator+"test.txt");
10 Writer out=new OutputStreamWriter(new FileOutputStream(f));
11 out.write("Hello World!!!");
12 out.close();
13 }
14 }
The object of the byte stream can also be read in the form of a byte stream.
1 import java.io.File;
2 import java.io.FileInputStream;
3 import java.io.IOException;
4 import java.io.InputStreamReader;
5 import java.io.Reader;
6
7 public class Test21 {
8 public static void main(String[] args) throws IOException {
9 File f = new File("d:" + File.separator+"test.txt");
10 Reader input=new InputStreamReader(new FileInputStream(f));
11 char[] c=new char[1024];
12 int len=input.read(c);
13 input.close();
14 System.out.println(new String(c,0,len));
15 }
16 }
The preceding operations take file operations as an example. OutputStreamWriter accepts OutputStream as the type. As long as it is a byte output stream, it can be operated in the form of characters, just like InputStreamReader.
Description of FileWriter and FileReader
From the JDK documentation, we can know that FileOutputStream is a direct subclass of OutputStream. FileInputStream is also a direct subclass of InputStream, but there are some special operation classes in the two operation classes of the volume stream file. FileWriter is not a subclass of Writer, but a subclass of OutputStreamWriter, fileReader is not directly a sub-class of Reader, but a sub-class of InputStreamReader, so we can clearly find the inheritance relationship between the two classes, whether byte streams or two streams are used, the input and output streams are operated in bytes. That is to say, when data is transmitted or read from a file, the truly stored data in the file will always be bytes.