📄 pipedemo.java
字号:
package javaio;
import java.io.*;
public class PipeDemo
{
static PipedInputStream pipedIS = new PipedInputStream();
static PipedOutputStream pipedOS = new PipedOutputStream();
public static void main(String[] args)
{
try
{
pipedIS.connect(pipedOS);
}
catch (IOException e)
{
System.err.println("连接失败");
System.exit(1);
}
byte[] inArray = new byte[10];
int bytesRead = 0;
// 启动写操作线程
startWriterThread();
try
{
bytesRead = pipedIS.read(inArray, 0, 10);
while (bytesRead != -1)
{
System.out.println("已经读取" + bytesRead + "字节...");
bytesRead = pipedIS.read(inArray, 0, 10);
}
}
catch (IOException e)
{
System.err.println("读取输入错误.");
System.exit(1);
}
}
// 创建一个独立的线程
// 执行写入PipedOutputStream的操作
private static void startWriterThread()
{
new Thread(new Runnable()
{
public void run()
{
byte[] outArray = new byte[2000];
while (true)
{ // 无终止条件的循环
try
{
// 在该线程阻塞之前,有最多1024字节的数据被写入
pipedOS.write(outArray, 0, 2000);
}
catch (IOException e)
{
System.err.println("写操作错误");
System.exit(1);
}
System.out.println(" 已经发送2000字节...");
}
}
}).start();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -