iodemo8.java

来自「JAVA的一些基础教程」· Java 代码 · 共 61 行

JAVA
61
字号
import java.io.*;

public class IODemo8 
	{
	static PipedInputStream 管道输入 = new PipedInputStream();
	static PipedOutputStream 管道输出 = new PipedOutputStream();

	public static void main(String[] args)
	 {
		try 
		{
			管道输入.connect(管道输出);
		}catch(IOException e) 
		{
			System.err.println("连接失败");
			System.exit(1);
		}

		byte[] 读取数据 = new byte[1000];
		int 读取字节数 = 0;
		int i = 0;
		启动写线程();
		try 
			{
			读取字节数 = 管道输入.read( 读取数据);
			while(读取字节数 != -1) {
			i++;
			if(i == 10) System.exit(0);
			System.out.println("已经读取" + 读取字节数 + "字节,还有" + 管道输入.available() + " 个字节");
			读取字节数 = 管道输入.read(读取数据);
			}
		}catch(IOException e) 
		{
			System.err.println("读取输入错误.");
		}
   	 }

	public static void 启动写线程() 
		{
		Runnable r = new Runnable()
			{
			public void run() 
				{
				byte[] 输出数据 = new byte[2000];
				while(true) {
			                  	try 
						{
			                       	 // 在该线程阻塞之前,有最多1024字节的数据被写入
					         管道输出.write(输出数据);
					}catch(IOException e) 
					{
						System.err.println("写操作错误");
					}
					System.out.println("     已经发送2000字节...");
				}
			}		
		};
		new Thread(r).start();	
    } 
} 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?