java 作家或输出流?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5223760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writer or OutputStream?
提问by yegor256
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)?
我正在设计一个库,其中一个类应该能够将自身内部结构转换为文本。我应该使用哪个类:OutputStream
或Writer
? 它们之间的主要区别是什么(就我而言)?
public interface Memento {
void save(OutputStream stream);
void save(Writer writer);
}
Which one?
哪一个?
回答by Ted Hopp
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.
AnOutputStream
是面向字节的流。您编写的任何文本都必须使用某种编码(最常见的是 ISO-8859-1 或 UTF-8)编码为字节。AWriter
是一个面向字符的流,它可能会或可能不会在内部将字符编码为字节,具体取决于它写入的内容。
EDITIf 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.
编辑如果你正在设计一个库,那么如果你提供一个OutputStream
面向的接口来写入文本,你真的应该为客户端类提供控制要使用的编码的能力。
回答by Tomasz Nurkiewicz
Text? Writer
. It is intended for handling characters, honors encoding.
文本?Writer
. 它用于处理字符,尊重编码。
Stream/array of bytes? OutputStream
. Works on raw bytes, has no notion of characters, encodings, strings, etc.
流/字节数组?OutputStream
. 适用于原始字节,没有字符、编码、字符串等概念。
回答by mentallurg
1) In many cases preferable can be overriding toString()or providing a similar method to convert ... internals into text. The advantagefor user of such method is flexibility. For instance, if the consumer of such method can:
1) 在许多情况下,更可取的方法是覆盖toString()或提供类似的方法将 ... 内部结构转换为 text。这种方法的优点是灵活性。例如,如果这种方法的使用者可以:
- Save it as a part of some POJO to database
- Incorporate it as a field into some JSON object
- Save to a stream
- Save it to a writer
- 将其作为某些 POJO 的一部分保存到数据库
- 将其作为字段合并到某个 JSON 对象中
- 保存到流
- 保存给作者
It can be disadvantage in some cases, e.g. when the text representation is relatively big (like 100 MB) and there are many requests simultaneously that produce such objects. This can require too much resources (CPU, RAM). In such case direct writing to a stream or to a writer can be preferable.
在某些情况下它可能是不利的,例如当文本表示相对较大(如 100 MB)并且同时有许多请求产生此类对象时。这可能需要太多资源(CPU、RAM)。在这种情况下,直接写入流或写入器可能更可取。
2) If you expect that you object can be used in many different contexts, then it makes sense to provide both, saving to a stream and saving to a writer. For instance, HttpServletResponseprovides both, getWriter()and getOutputStream(), so that everyone can decide what is better in the his particular case. Or Hymanson's JsonFactoryprovides methods * createGenerator()* for File, OutputStreamand Writer, giving the consumer much freedom.
2)如果你希望你的对象可以在许多不同的环境中使用,那么它是有道理的,提供两个,保存到一个流并将其保存到一个作家。例如,HttpServletResponse提供了getWriter()和getOutputStream(),这样每个人都可以决定在他的特定情况下哪个更好。或者 Hymanson 的JsonFactory为File、OutputStream和Writer提供了方法 * createGenerator()* ,给了消费者很大的自由。