我应首先转换字节流,缓冲区流或字符流? [英] Which shall I convert a byte stream first, buffer stream or char stream?
问题描述
要获得给定字节流的缓冲char流,我应将字节流转换为第一个,缓冲流还是char流?
To get a buffered char stream for a given byte stream, which shall I convert the byte stream to first, a buffer stream or a char stream?
-
给出一个
OutputStream
对象os
(例如FileOutputStream
对象或System.out
),似乎有两种方法可以获取 缓冲的字符流:
Given an
OutputStream
objectos
(such asFileOutputStream
object orSystem.out
), there seems to be two ways to get a buffered char stream:
new BufferedWriter(new OutputStreamWriter(os));
new OutputStreamWriter(new BufferedOutputStream(os));
它们之间有什么区别?
给出一个InputStream
对象is
(例如FileInputStream
对象
或System.in
),似乎有两种方法可以获取缓冲的char
流:
Given an InputStream
object is
(such as FileInputStream
object
or System.in
), there seems to be two ways to get a buffered char
stream:
new BufferedReader(new InputStreamReader(is));
new InputStreamReader(new BufferedInputStream(is));
它们之间有什么区别?
推荐答案
- 如javadoc中针对 OutputStreamWriter 所述>
- As stated in javadoc for OutputStreamWriter
传递到write()方法的字符不被缓冲.
为获得最高效率,请考虑将OutputStreamWriter包装在BufferedWriter中,以避免频繁的转换器调用.例如:
Writer out = new BufferedWriter(new OutputStreamWriter(System.out));
The characters passed to the write() methods are not buffered.
For top efficiency, consider wrapping an OutputStreamWriter within a BufferedWriter so as to avoid frequent converter invocations. For example:
Writer out = new BufferedWriter(new OutputStreamWriter(System.out));
由于转换后的结果字节已累积在缓冲区中,因此不需要在OutputStreamWriter之后通过管道插入BufferedStream
Since resulting bytes after conversion are accumulated in a buffer, there is no need to in-pipe BufferedStream after OutputStreamWriter
因此,常规方法是使用:
Thus, the conventional way is to use:
new BufferedWriter(new OutputStreamWriter(os));
- InputStreamReader 的类似原因
使用:
new BufferedReader(new InputStreamReader(is));
这篇关于我应首先转换字节流,缓冲区流或字符流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!