Java.io.PipedInputStream.read() 方法
描述
java.io.PipedInputStream.read() 方法从此管道输入流中读取数据的下一个字节。 值字节以 0 到 255 范围内的 int 形式返回。此方法阻塞,直到输入数据可用、检测到流结束或引发异常。
声明
以下是 java.io.PipedInputStream.read() 方法的声明。
public int read()
参数
NA
返回值
此方法返回数据的下一个字节,如果到达流的末尾,则返回 -1。
异常
IOException − 如果发生 I/O 错误。
示例
下面的例子展示了 java.io.PipedInputStream.read() 方法的使用。
package com.tutorialspoint;
import java.io.*;
public class PipedInputStreamDemo {
public static void main(String[] args) {
// create a new Piped input and Output Stream
PipedOutputStream out = new PipedOutputStream();
PipedInputStream in = new PipedInputStream();
try {
// connect input and output
in.connect(out);
// write something
out.write(70);
out.write(71);
// read what we wrote
for (int i = 0; i < 2; i++) {
System.out.println("" + (char) in.read());
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
F G

