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
PrintStream vs PrintWriter
提问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
PrintStream
was the original bridge to deal with encoding characters and other datatypes. If you look at the javadoc for java.io.OutputStream
you'll see methods only for writing two distinct data types: byte
and int
.
PrintStream
是处理编码字符和其他数据类型的原始桥梁。如果您查看 javadoc,java.io.OutputStream
您将看到仅用于编写两种不同数据类型的方法:byte
和int
。
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 outputStream
in a PrintStream
:
或将另一个包装outputStream
在一个PrintStream
:
PrintStream printStream = new PrintStream(outputStream);
printStream.write("foobar");
See the difference? PrintStream
is 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
正在处理字符到字节的转换以及编码(上面的构造函数调用使用系统默认编码,但您可以将其作为参数传递)。它还提供了便利的方法写作double
,boolean
等等....
In fact System.out
and System.err
are defined as PrintStream
instances.
事实上System.out
和System.err
被定义为PrintStream
实例。
Along comes JDK 1.1, and they realize they need a better way to deal with pure character data, since PrintStream
still has the byte based methods for writing. So they introduced the Writer
abstract class to deal strictly with char
, String
and int
data.
随着 JDK 1.1 的出现,他们意识到他们需要一种更好的方法来处理纯字符数据,因为PrintStream
仍然有基于字节的写入方法。所以他们引入了Writer
抽象类来严格处理char
,String
和int
数据。
PrintWriter
adds methods for other types like double
, boolean
, etc...
PrintWriter
增加了对其他类型的方法,如double
,boolean
等...
Nowadays PrintWriter
also has format()
/ printf()
methods for format printing, etc...
现在PrintWriter
也有format()
/printf()
格式打印的方法等......
As a general rule, if you're writing character data, use Writer
instances. If you're writing binary (or mixed) data use OutputStream
instances.
作为一般规则,如果您正在编写字符数据,请使用Writer
实例。如果您正在编写二进制(或混合)数据使用OutputStream
实例。
回答by Dilum Ranatunga
From the Javadoc for PrintWriter:
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 PrintStream
sits 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 File
s and OutputStream
s, but those are simply conveniences. For example, PrintWriter(OutputStream)
.
一PrintWriter
,在另一方面,坐落在的顶部Writer
。由于 Writer 负责将字符编码为字节,因此 PrintWriter 不进行编码。我只知道换行符等(是的,PrintWriters 确实有接受File
s 和OutputStream
s 的构造函数,但这些只是方便。例如,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 PrintWriter
really doesn't have much utility, remember that both PrintWriter and PrintStream absorb IOException
s from printing logic.
顺便说一句,如果您认为sPrintWriter
确实没有太多实用性,请记住 PrintWriter 和 PrintStream 都IOException
从打印逻辑中吸收s。
回答by 18446744073709551615
To add Matt's answer:
添加马特的答案:
I compared PrintStream
and PrintWriter
,
the most useful part, the constructor ClassName
(String fileName, String charsetName)
and the print(), println(), printf()/format()functions are supported by both classes.
我比较了PrintStream
和PrintWriter
,最有用的部分是构造函数和两个类都支持的print()、println()、printf()/format()函数。ClassName
(String fileName, String charsetName)
The differences are:
区别在于:
Since JDK1.0
vs JDK1.1
由于JDK1.0
与JDK1.1
Constructors:
构造函数:
PrintStream(OutputStream out, boolean autoFlush, String charsetName)
PrintWriter(Writer wr)
PrintWriter(Writer wr, boolean autoFlush)
Methods inherited from FilterOutputStream
/OutputStream
vs Writer
, the difference boils down to byte
vs char
:
从FilterOutputStream
/ OutputStream
vs继承的方法Writer
,差异归结为byte
vs 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 类。