PrintWriter缓冲? [英] Is PrintWriter buffered?

查看:173
本文介绍了PrintWriter缓冲?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 PrintWriter 是非常好的,如果我们要写格式化的数据,我也知道使用 BufferedWriter

但我试过这样的事情,

  PrintWriter pw = new PrintWriter(System.out); 
pw.println(Statement 1);
pw.println(Statement 2);
//pw.flush();

我观察到当flush方法被评论时没有输出,但是当我取消注释时,获得所需的输出。



只有在PrintWriter被缓冲的情况下才可以使用。如果是这样,那么打印PrintWriter一个BufferedWriter,然后写它?



尽管javadoc没有提及PrintWriter缓冲的地方,但似乎是这样。我检查了从1.6.0_45开始的JDK版本,并且它们都有

com / javase / 8 / docs / api / java / io / PrintWriter.html#PrintWriter-java.io.Writer-boolean-rel =nofollow>这个构造函数存在:

  / ** 
*从现有的OutputStream中创建一个新的PrintWriter。这个
*便利构造函数创建必要的中间
* OutputStreamWriter,它将使用
*默认字符编码将字符转换为字节。
*
* @param输出流
* @param autoFlush一个布尔值;如果是true,那么< tt> println< / tt> ;,
*< tt> printf< / tt>或< tt>格式< / tt>方法将
刷新输出缓冲区
*
* @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
* /
public PrintWriter OutputStream out,boolean autoFlush){
this(new BufferedWriter(new OutputStreamWriter(out)),autoFlush);

因此PrintWritter使用缓冲输出。如果您想使用您指出的代码,可以使用 autoflush 设置为 true 来创建PrintWriter,这将确保使用 println printf format 方法会刷新流。所以,你的代码在给定的上下文中看起来像这样:
$ b

  PrintWriter pw = new PrintWriter(System.out,true); 
pw.println(Statement 1);
pw.println(Statement 2);


I know that the PrintWriter is really good if we want to write formatted data, and I also know the use of BufferedWriter to improve IO performance.

But I tried something like this,

PrintWriter pw = new PrintWriter(System.out);
pw.println("Statement 1");
pw.println("Statement 2");
//pw.flush();

I observed that when the flush method is commented there is no output, but when I uncomment it, I get the desired output.

This is only possible if the PrintWriter is buffered. If so, then what is the point of wrapping a PrintWriter using a BufferedWriter and then writing it?

Though the javadoc doesn't mention anywhere that the PrintWriter is buffered, but it seems so.

解决方案

I checked JDK versions starting with 1.6.0_45 and all of them have this constructor present:

/**
 * Creates a new PrintWriter from an existing OutputStream.  This
 * convenience constructor creates the necessary intermediate
 * OutputStreamWriter, which will convert characters into bytes using the
 * default character encoding.
 *
 * @param  out        An output stream
 * @param  autoFlush  A boolean; if true, the <tt>println</tt>,
 *                    <tt>printf</tt>, or <tt>format</tt> methods will
 *                    flush the output buffer
 *
 * @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
 */
public PrintWriter(OutputStream out, boolean autoFlush) {
this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);

Hence PrintWritter uses buffered output. If you would like to use the code you pointed out, you can create the PrintWriter with the autoflush set to true, that will ensure that using one of the println, printf or format methods would flush the stream. So, your code would look like this in the given context:

PrintWriter pw = new PrintWriter(System.out, true);
pw.println("Statement 1");
pw.println("Statement 2");

这篇关于PrintWriter缓冲?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆