Writer或OutputStream? [英] Writer or OutputStream?
问题描述
我正在设计一个库,其中一个类应该能够将自己的内部转换为文本。我应该使用哪个类: OutputStream
或 作家
?它们之间的关键区别是什么(在我的情况下)?
I'm designing a library where a class should have an ability to be able to convert itself internals into text. Which class shall I use: OutputStream
or Writer
? And what is the key difference between them (in my case)?
public interface Memento {
void save(OutputStream stream);
void save(Writer writer);
}
哪一个?
推荐答案
OutputStream
是一个面向字节的流。您编写的任何文本都必须使用某种编码(最常见的是ISO-8859-1或UTF-8)编码为字节。 Writer
是一个面向字符的流,可能会也可能不会将字符内部编码为字节,具体取决于它写入的内容。
An OutputStream
is a byte-oriented stream. Any text you write has to be encoded as bytes using some encoding (most commonly ISO-8859-1 or UTF-8). A Writer
is a character-oriented stream that may or may not internally encode characters as bytes, depending on what it is writing to.
编辑如果您正在设计库,那么如果您提供要写入文本的 OutputStream
-oriented接口,你真的应该为客户端类提供控制要使用的编码的能力。
EDIT If you are designing a library, then if you provide an OutputStream
-oriented interface to which text is to be written, you really should provide client classes the ability to control the encoding to be used.
这篇关于Writer或OutputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!