?? 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 + -