Java PipedReader close()方法

java.io.PipedReader.close() 方法将指定字节写入到管道输出流。实现OutputStream的write方法。

1 语法

public void close()

2 参数

3 返回值

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.io.PipedReader.close()方法的例子
 */
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
            reader.connect(writer);

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

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

            // close the reader
            System.out.println("Closing reader...");
            reader.close();
            System.out.println("Reader closed.");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

输出结果为:

F
G
Closing reader...
Reader closed.

热门文章

优秀文章