java中字符流和字节流的转换
ringa_lee
ringa_lee 2017-04-17 17:37:42
0
3
943

看到InputStreamReader和OutputStreamWriter的一些说法,
说InputStreamReader是将字节流转换成字符流,而OutputStreamWriter则相反,是将字符流转换成字节流;
但是
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("xxxx")));
这句代码中,明显OutputStreamWriter接收的是一个字节流,而返回的则是个字符流,
所以上面的说法应该是错误的吧?

ringa_lee
ringa_lee

ringa_lee

reply all(3)
刘奇

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...

PHPzhong
    /**
     * String字符串转换成InputStream流
     * @param str String字符串
     * @param encoding 编码格式
     * @return InputStream流
     */
    public static InputStream string2InputStream(String str, String encoding) {
        ByteArrayInputStream result = null;
        try {
            if(str != null){
                result = new ByteArrayInputStream(str.getBytes(encoding));
            }
        } catch (Exception e) {
            result = null;
        }
        return result;
    }
    /**
     * InputStream流转换成String字符串
     * @param inStream InputStream流
     * @param encoding 编码格式
     * @return String字符串
     */
    public static String inputStream2String(InputStream inStream, String encoding){
        String result = null;
        try {
            if(inStream != null){
                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                byte[] tempBytes = new byte[_buffer_size];
                int count = -1;
                while((count = inStream.read(tempBytes, 0, _buffer_size)) != -1){
                    outStream.write(tempBytes, 0, count);
                }
                tempBytes = null;
                outStream.flush();
                result = new String(outStream.toByteArray(), encoding);
            }
        } catch (Exception e) {
            result = null;
        }
        return result;
    }
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template