【java基础:IO】管道流PipedInputStream和PipedOutputStream的Demostration


package pipedinputouput;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class PipedInputOutputStreamDemo {
	
	/*
	 * 需求:分别为管道流的读和写开辟线程!
	 */
	public static void main(String[] args) throws IOException {
		PipedInputStream pis=new PipedInputStream();
		PipedOutputStream pos=new PipedOutputStream();
		pis.connect(pos);
		new Thread(new Input(pis)).start();
		new Thread(new Output(pos)).start();
	}	
}
class Input implements Runnable{
	private PipedInputStream pis;
	public Input(PipedInputStream pis) {
		super();
		this.pis = pis;
	}
	@Override
	public void run() {
		try {
			byte[] buf=new byte[1024];
			int len=pis.read(buf);
			String str=new String(buf,0,len);	
			System.out.println(str);
		}
		catch(Exception e){	
		}
		finally {
			try {
				pis.close();
			}catch (IOException e) {			
				e.printStackTrace();
			}
		}
	}
}
class Output implements Runnable{
	private PipedOutputStream pos;
	public Output(PipedOutputStream pos) {
		super();
		this.pos = pos;
	}
	@Override
	public void run() {
		try {
			Thread.sleep(3000);
			pos.write("这是管道流的信息,O(∩_∩)O~".getBytes());
		} catch(Exception e) {
			//
		}
	}
}


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com 联系我们: