java PrintStream 与 PrintWriter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11372546/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:48:43  来源:igfitidea点击:

PrintStream vs PrintWriter

java

提问by i'mhungry

I have searched the site and have found some answers, but i'm having trouble understanding the difference between these two classes. Can someone explain the differences between these two classes?

我搜索了该站点并找到了一些答案,但是我无法理解这两个类之间的区别。有人可以解释这两个类之间的区别吗?

回答by Matt

PrintStreamwas the original bridge to deal with encoding characters and other datatypes. If you look at the javadoc for java.io.OutputStreamyou'll see methods only for writing two distinct data types: byteand int.

PrintStream是处理编码字符和其他数据类型的原始桥梁。如果您查看 javadoc,java.io.OutputStream您将看到仅用于编写两种不同数据类型的方法:byteint

In early versions of the JDK (1.0.x), when you wanted to write characters, you could do one of two things, write bytes to an output stream (which are assumed to be in the system default character set):

在 JDK 的早期版本 (1.0.x) 中,当您想要写入字符时,您可以执行以下两种操作之一,将字节写入输出流(假定它们在系统默认字符集中):

outputStream.write("foobar".getBytes());

or wrap another outputStreamin a PrintStream:

或将另一个包装outputStream在一个PrintStream

PrintStream printStream = new PrintStream(outputStream);
printStream.write("foobar");

See the difference? PrintStreamis handling the character conversion to bytes, as well as encoding (the constructor call above uses the system default encoding, but you could pass it as a parameter). It also provides convenience methods for writing double, boolean, etc....

看到不同? PrintStream正在处理字符到字节的转换以及编码(上面的构造函数调用使用系统默认编码,但您可以将其作为参数传递)。它还提供了便利的方法写作doubleboolean等等....

In fact System.outand System.errare defined as PrintStreaminstances.

事实上System.outSystem.err被定义为PrintStream实例。

Along comes JDK 1.1, and they realize they need a better way to deal with pure character data, since PrintStreamstill has the byte based methods for writing. So they introduced the Writerabstract class to deal strictly with char, Stringand intdata.

随着 JDK 1.1 的出现,他们意识到他们需要一种更好的方法来处理纯字符数据,因为PrintStream仍然有基于字节的写入方法。所以他们引入了Writer抽象类来严格处理char,Stringint数据。

PrintWriteradds methods for other types like double, boolean, etc...

PrintWriter增加了对其他类型的方法,如doubleboolean等...

Nowadays PrintWriteralso has format()/ printf()methods for format printing, etc...

现在PrintWriter也有format()/printf()格式打印的方法等......

As a general rule, if you're writing character data, use Writerinstances. If you're writing binary (or mixed) data use OutputStreaminstances.

作为一般规则,如果您正在编写字符数据,请使用Writer实例。如果您正在编写二进制(或混合)数据使用OutputStream实例。

回答by Dilum Ranatunga

From the Javadoc for PrintWriter:

来自PrintWriterJavadoc

Prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.

将对象的格式化表示打印到文本输出流。此类实现在 PrintStream 中找到的所有打印方法。它不包含写入原始字节的方法,程序应该使用未编码的字节流。

Think of it this way: a PrintStreamsits on top of some OutputStream. Since output streams deal with bytes rather than characters, the PrintStream musttake responsibility for encoding characters into bytes. The OutputStream 'merely' writes the bytes out to a file/console/socket whatever.

可以这样想: aPrintStream位于 some 之上OutputStream。由于输出流处理字节而不是字符,因此 PrintStream必须负责将字符编码为字节。OutputStream“仅仅”将字节写入文件/控制台/套接字。

A PrintWriter, on the other hand, sits on top of a Writer. Since the Writer is responsible for encoding characters into bytes, the PrintWriter does not do encoding. I just knows about newlines etc. (Yes, PrintWriters do have constructors that take Files and OutputStreams, but those are simply conveniences. For example, PrintWriter(OutputStream).

PrintWriter,在另一方面,坐落在的顶部Writer。由于 Writer 负责将字符编码为字节,因此 PrintWriter 不进行编码。我只知道换行符等(是的,PrintWriters 确实有接受Files 和OutputStreams 的构造函数,但这些只是方便。例如,PrintWriter(OutputStream).

Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert characters into bytes using the default character encoding.

从现有的 OutputStream 创建一个没有自动行刷新的新 PrintWriter。这个方便的构造函数创建了必要的中间 OutputStreamWriter,它将使用默认字符编码将字符转换为字节。

BTW, In case you are thinking that the PrintWriterreally doesn't have much utility, remember that both PrintWriter and PrintStream absorb IOExceptions from printing logic.

顺便说一句,如果您认为sPrintWriter确实没有太多实用性,请记住 PrintWriter 和 PrintStream 都IOException从打印逻辑中吸收s。

回答by 18446744073709551615

To add Matt's answer:

添加马特的答案:

I compared PrintStreamand PrintWriter, the most useful part, the constructor ClassName(String fileName, String charsetName)and the print(), println(), printf()/format()functions are supported by both classes.

我比较了PrintStreamPrintWriter,最有用的部分是构造函数和两个类都支持的print()、println()、printf()/format()函数。ClassName(String fileName, String charsetName)

The differences are:

区别在于:

Since JDK1.0vs JDK1.1

由于JDK1.0JDK1.1

Constructors:

构造函数:

PrintStream(OutputStream out, boolean autoFlush, String charsetName)
PrintWriter(Writer wr)
PrintWriter(Writer wr, boolean autoFlush)

Methods inherited from FilterOutputStream/OutputStreamvs Writer, the difference boils down to bytevs char:

FilterOutputStream/ OutputStreamvs继承的方法Writer,差异归结为bytevs char

PrintStream.write(byte[] buffer, int offset, int count)
PrintStream.write(byte[] buffer)
PrintStream.write(int oneByte)

PrintWriter.write(int oneChar)
PrintWriter.write(char[] buf)
PrintWriter.write(char[] buf, int offset, int count)
PrintWriter.write(String str)
PrintWriter.write(String str, int offset, int count)

PrintStream.printf()corresponds to PrintWriter.format()

PrintStream.printf()对应于 PrintWriter.format()

It indeed looks like in 1.1. they thought out a better class, but could not remove the old 1.0 class without breaking existing programs.

它确实看起来像在 1.1 中。他们想出了一个更好的类,但无法在不破坏现有程序的情况下删除旧的 1.0 类。