Java:PrintStream 和 PrintWriter 之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2822005/
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
Java: Difference between PrintStream and PrintWriter
提问by Martijn Courteaux
What is the difference between PrintStream
and PrintWriter
? They have many methods in common due to which I often mix these two classes up. Moreover, I think we can use them for exactly the same things. But there has to be a difference, otherwise, there would have been only one class.
PrintStream
和 和有PrintWriter
什么区别?它们有许多共同的方法,因此我经常将这两个类混合在一起。此外,我认为我们可以将它们用于完全相同的事情。但必须有区别,否则,就只有一个班级。
I have searched the archives, but couldn't find this question.
我搜索了档案,但找不到这个问题。
采纳答案by mdma
This might sound flippant, but PrintStream
prints to an OutputStream
, and PrintWriter
prints to a Writer
. Ok, I doubt I'll get any points for stating the obvious. But there's more.
这听起来可能很轻率,但PrintStream
打印到OutputStream
,然后PrintWriter
打印到Writer
。好吧,我怀疑我会因为陈述明显的问题而得到任何分数。但还有更多。
So, what is the difference between an OutputStream
and a Writer
?
Both are streams, with the primary difference being a OutputStream
is a stream of bytes while a Writer
is a stream of characters.
那么,anOutputStream
和 a之间有什么区别Writer
?两者都是流,主要区别是 aOutputStream
是字节流,而 aWriter
是字符流。
If an OutputStream
deals with bytes, what about PrintStream.print(String)
? It converts chars to bytes using the default platform encoding. Using the default encoding is generally a bad thing since it can lead to bugs when moving from one platform to another, especially if you are generating the file on one platform and consuming it on another.
如果 aOutputStream
处理字节,那PrintStream.print(String)
呢?它使用默认平台编码将字符转换为字节。使用默认编码通常是一件坏事,因为它会在从一个平台移动到另一个平台时导致错误,尤其是当您在一个平台上生成文件并在另一个平台上使用它时。
With a Writer
, you typically specify the encoding to use, avoiding any platform dependencies.
使用Writer
,您通常指定要使用的编码,避免任何平台依赖性。
Why bother having a PrintStream
in the JDK, since the primary intent is to write characters, and not bytes? PrintStream
predates JDK 1.1 when Reader/Writer character streams were introduced. I imagine Sun would have deprecated PrintStream
if only for the fact it is so widely used. (After all, you wouldn't want each call to System.out
to generate a deprecated API warning! Also, changing the type from PrintStream
to PrintWriter
on the standard output streams would have broken existing applications.)
为什么要PrintStream
在 JDK 中使用 a ,因为主要目的是写入字符而不是字节?PrintStream
当引入 Reader/Writer 字符流时,它早于 JDK 1.1。我想PrintStream
如果仅仅因为它被如此广泛地使用,Sun 就会被弃用。(毕竟,你不希望每次调用System.out
生成一个过时的API的警告!另外,从变更类型PrintStream
,以PrintWriter
在标准输出流将会破坏现有的应用程序。)
回答by sblundy
Writers like PrintWriter are for text output, streams are for binary output. The writers handle character set stuff for you. Streams don't because it's assumed that you don't want that sort of conversion, which would mess up your binary data, and would be using a writer if you did.
像 PrintWriter 这样的编写器用于文本输出,流用于二进制输出。作家为您处理字符集。Streams 不会,因为它假定您不想要那种转换,这会弄乱您的二进制数据,并且如果您这样做将使用编写器。
回答by Simon Groenewolt
You can write raw bytes to a Stream and not to a Writer. The PrintWriterjavadoc lists the other differences (most importantly, being able to set an encoding on a stream so it can interpret the raw bytes I'd say).
您可以将原始字节写入 Stream 而不是写入 Writer。该PrintWriter的javadoc中列出了其他方面的差异(最重要的是,能够设置一个流上的编码,因此它可以解释原始字节我会说)。
回答by BalusC
With the PrintStream
you're stuck to platform's default encoding.
随着PrintStream
你坚持平台的默认编码。
PrintStream stream = new PrintStream(output);
With the PrintWriter
you can however pass an OutputStreamWriter
with a specific encoding.
PrintWriter
但是,您可以传递OutputStreamWriter
具有特定编码的 。
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, "UTF-8"));
The advantage is, well, that you can control the character encoding the characters should be written in such that they won't eventually end up as mojibake.
好处是,你可以控制字符编码应该写入的字符,这样它们最终不会成为mojibake。
回答by Renan Mozone
Since JDK 1.4 it's possible to specify the character encoding for a PrintStream. Thus, the differences between PrintStream and PrintWriter are only about auto flushing behavior and that a PrintStream cannot wrap a Writer.
从 JDK 1.4 开始,可以为 PrintStream 指定字符编码。因此,PrintStream 和 PrintWriter 之间的区别仅在于自动刷新行为,并且 PrintStream 不能包装 Writer。
回答by Spoo
Printwriter is an enhancement of printstream.
Printwriter 是对打印流的增强。
I.E. printstream for a specific purpose.
用于特定目的的 IE 打印流。
回答by nichijou
from core javaby Horstmann
来自Horstmann 的核心 Java
Java veterans might wonder whatever happened to the PrintStream class and to System.out. In Java 1.0, the PrintStream class simply truncated all Unicode characters to ASCII characters by dropping the top byte. (At the time, Unicode was still a 16-bit encoding.) Clearly, that was not a clean or portable approach, and it was fixed with the introduction of readers and writers in Java 1.1. For compatibility with existing code, System.in, System.out, and System.err are still input/output streams, not readers and writers. But now the PrintStream class internally converts Unicode characters to the default host encoding in the same way the PrintWriter does. Objects of type PrintStream act exactly like print writers when you use the print and println methods, but unlike print writers they allow you to output raw bytes with the write(int) and write(byte[]) methods.
Java 老手可能想知道 PrintStream 类和 System.out 发生了什么。在 Java 1.0 中,PrintStream 类只是通过删除顶部字节将所有 Unicode 字符截断为 ASCII 字符。(当时,Unicode 仍然是 16 位编码。)显然,这不是一种干净或可移植的方法,并且随着 Java 1.1 中读取器和写入器的引入而得到修复。为了与现有代码兼容,System.in、System.out 和 System.err 仍然是输入/输出流,而不是读取器和写入器。但是现在 PrintStream 类以与 PrintWriter 相同的方式在内部将 Unicode 字符转换为默认主机编码。当您使用 print 和 println 方法时, PrintStream 类型的对象的行为与打印编写器完全相同,