看到InputStreamReader和OutputStreamWriter的一些说法,
说InputStreamReader是将字节流转换成字符流,而OutputStreamWriter则相反,是将字符流转换成字节流;
但是
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("xxxx")));
这句代码中,明显OutputStreamWriter接收的是一个字节流,而返回的则是个字符流,
所以上面的说法应该是错误的吧?
Um...this...
The input parameter of the constructor method of OutputStreamWriter is an OutputStream byte stream, which means converting the OutputStream into an OutputStreamWriter. From this perspective, it is "converting the byte stream into a character stream".
When OutputStreamWriter writes, you pass in characters, but in the end IO writes a byte stream. From this perspective, it is "converting the character stream into a byte stream".
So both statements are correct.
"Convert byte stream to character stream" refers to the conversion of API, "Convert character stream to byte stream" refers to the conversion of data during write.
In fact, you only need to know that the computer itself only supports binary bytes such as 01010101, and characters or strings are obtained by byte encoding. OutputStreamWriter is just one more encoding operation.
The OutputStreamWriter is constructed from a FileOutputStream. This stream is actually encapsulated in the writer. All write operations to the writer will eventually be written to the stream. But when writing to the writer, what is written is a string, and the writer will convert the incoming string into a character stream and write it into stream... So...