简体   繁体   English

PrintWriter 如何写入字节流,而 BufferedWriter 不能?

[英]How PrintWriter can write to byte stream, but BufferedWriter cannot?

To start with I will provide a piece of code with comments:首先,我将提供一段带有注释的代码:

FileOutputStream fos = new FileOutputStream ("test.txt");
//fos IS-A OutputStream as FileOutputStream extends OutputStream

PrintWriter pw = new PrintWriter (fos); //compiles (can take OutputStream)

BufferedWriter bw = new BufferedWriter (fos); //doesn't compile (can't take OutputStream)
//Need to do this:
BufferedWriter bufferedWriter = new BufferedWriter (new OutputStreamWriter (fos)); //compiles

I thought I knew I/O streams until I came across this mistake.我以为我知道I/O 流,直到我遇到这个错误。 Can someone explain how PrintWriter can write to a byte stream , while BufferedWriter cannot?有人可以解释PrintWriter如何写入字节流,而BufferedWriter不能吗? Shouldn't they be the same?他们不应该是一样的吗? I always saw them as equal (meaning on internal implementation), but the difference was in methods they possess.我一直认为它们是平等的(意味着内部实现),但不同之处在于它们拥有的方法。

Looks I was wrong :) I see it this way: BufferedWriter can't write to byte stream as it is a character stream, meaning someone has to convert those characters.看起来我错了:) 我是这样看的: BufferedWriter不能写入字节流,因为它是一个字符流,这意味着必须有人转换这些字符。 That is why we 'give' OutputStreamWriter as a bridge from character to byte streams .这就是为什么我们将OutputStreamWriter作为从字符流到字节流桥梁 So, how come PrintWriter doesn't need this "bridge".那么,为什么PrintWriter不需要这个“桥梁”。 Couldn't they have a way of coding and make our lives easier?他们难道不能有一种编码方式让我们的生活更轻松吗? Or it is my fault for missing something?还是我错过了什么?

So, how come PrintWriter doesn't need this "bridge".那么,为什么 PrintWriter 不需要这个“桥梁”。

The javadocs for the PrintWriter(OutputStream, ...) constructors state that they are convenience constructors. PrintWriter(OutputStream, ...)构造函数的javadoc声明它们是便利构造函数。 In short, they provide a quick way to do something that you can do another way.简而言之,它们提供了一种快速的方式来做一些你可以用另一种方式做的事情。

Couldn't they have a way of coding and make our lives easier?他们难道不能有一种编码方式让我们的生活更轻松吗?

You are now referring to BufferedWriter not having a convenience constructor that takes an OutputStream argument.您现在指的是 BufferedWriter 没有接受OutputStream参数的便捷构造函数。

As a general rule, convenience constructors and methods are provided for use-cases that are thought to be common enough to warrant this.作为一般规则,为被认为足够常见以保证这一点的用例提供了便利的构造函数和方法。

In this case, they would potentially need to provide many convenience constructors:在这种情况下,他们可能需要提供许多方便的构造函数:

  • BufferedWriter(OutputStream) using the default charset. BufferedWriter(OutputStream)使用默认字符集。
  • BufferedWriter(OutputStream, String) supplying the charset name BufferedWriter(OutputStream, String)提供字符集名称
  • BufferedWriter(OutputStream, Charset) supplying the charset BufferedWriter(OutputStream, Charset)提供字符集
  • BufferedWriter(OutputStream, CharsetEncoder) supplying the charset encoder directly BufferedWriter(OutputStream, CharsetEncoder)直接提供字符集编码器

and possibly 4 more to supply non-default buffer sizes.可能还有 4 个用于提供非默认缓冲区大小。

I imagine they did not think this was justifiable.我想他们不认为这是合理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM