Java PipedWriter flush()方法

java.io.PipedWriter.flush() 方法刷新此输出流并强制写出所有缓冲的输出字符。这将通知字符守候在管道的任何读取器。

1 语法

public void flush()

2 参数

3 返回值

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.io.PipedWriter.flush()方法的例子
 */
import java.io.*;

public class Demo {
    public static void main(String[] args) {

        // create a new Piped writer and reader
        PipedWriter writer = new PipedWriter();
        PipedReader reader = new PipedReader();

        try {
            // connect the reader and the writer
            writer.connect(reader);

            // write something
            writer.write(70);
            writer.write(71);

            // flush the writer
            System.out.println("Flushing writer...");
            writer.flush();
            System.out.println("Writer flushed.");

            // print what we wrote
            for (int i = 0; i < 2; i++) {
                System.out.println("" + (char) reader.read());
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

输出结果为:

Flushing writer...
Writer flushed.
F
G

热门文章

优秀文章