java OutputStream 和 Writer 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10816554/
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
What is the difference between OutputStream and Writer?
提问by Ani
Can someone explain me the difference between OutputStream
and Writer
? Which of these classes should I work with?
有人可以解释我的区别OutputStream
和Writer
?我应该使用这些课程中的哪些?
回答by Vincent Robert
Streams work at the byte level, they can read (InputStream) and write (OutputStream) bytes or list of bytes to a stream.
流在字节级别工作,它们可以读取 ( InputStream) 和写入 ( OutputStream) 字节或字节列表到流。
Reader/Writers add the concept of character on top of a stream. Since a character can only be translated to bytes by using an Encoding, readers and writers have an encoding component (that may be set automatically since Java has a default encoding property). The characters read (Reader) or written (Writer) are automatically converted to bytes by the encoding and sent to the stream.
Reader/Writers 在流的顶部添加字符的概念。由于字符只能使用 Encoding 转换为字节,因此读取器和写入器具有编码组件(由于 Java 具有默认编码属性,因此可以自动设置)。读取 ( Reader) 或写入 ( Writer) 的字符会通过编码自动转换为字节并发送到流。
回答by Chandra Sekhar
OutputStreamclasses writes to the target byte by bytewhere as Writerclasses writes to the target character by character
OutputStream类逐字节写入目标,而Writer类逐个字符写入目标
回答by Thorsten Dittmar
An OutputStream
is a stream that can write information. This is fairly general, so there are specialized OutputStream
for special purposes like writing to files. A stream can only write arrays of bytes.
AnOutputStream
是可以写入信息的流。这是相当普遍的,因此有专门OutputStream
用于特殊目的,例如写入文件。流只能写入字节数组。
Writer
s provide more flexibility in that they can write characters and even strings while taking a special encoding into account.
Writer
s 提供了更大的灵活性,因为它们可以在考虑特殊编码的同时写入字符甚至字符串。
Which one to take is really a matter of what you want to write. If you do have bytes already, you can use the stream directly. If you have characters or strings, you either need to convert them to bytes yourself if you want to write them to a stream, or you need to use a Writer
which does that job for you.
拿哪一个真的是你想写什么的问题。如果您已经有字节,则可以直接使用流。如果您有字符或字符串,如果要将它们写入流,您要么需要自己将它们转换为字节,要么需要使用 aWriter
来为您完成这项工作。
回答by Abhishek Ranjan
The Reader/Writer class hierarchy is character-oriented, and the Input Stream/Output Stream class hierarchy is byte-oriented. Basically there are two types of streams.Byte streams that are used to handle stream of bytes and character streams for handling streams of characters.In byte streams input/output streams are the abstract classes at the top of hierarchy,while writer/reader are abstract classes at the top of character streams hierarchy.
Reader/Writer 类层次结构是面向字符的,而 Input Stream/Output Stream 类层次结构是面向字节的。基本上有两种类型的流。用于处理字节流的字节流和用于处理字符流的字符流。在字节流中,输入/输出流是层次结构顶部的抽象类,而写入器/读取器是抽象的字符流层次结构顶部的类。
Cheers!!!
干杯!!!
回答by DerMike
OutputStream
uses bare bytes, whereas Writer
uses encodedcharaters.
OutputStream
使用裸字节,而Writer
使用编码字符。