Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1092289
  • 博文数量: 252
  • 博客积分: 4561
  • 博客等级: 上校
  • 技术积分: 2833
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-15 08:23
文章分类

全部博文(252)

文章存档

2015年(2)

2014年(1)

2013年(1)

2012年(16)

2011年(42)

2010年(67)

2009年(87)

2008年(36)

分类: LINUX

2009-09-02 17:34:34

import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

class ReadThread extends Thread
{
    public PipedInputStream in;
    
    public ReadThread(PipedInputStream in)
    {
        this.in = in;
    }
    
    public void connect(PipedOutputStream out) throws Exception
    {
        in.connect(out);
    }
    
    public void run()
    {
        try
        {
            byte[] data = new byte[1024];
            int len;
            
            len = in.read(data, 0, 1024);
            in.close();
            
            DataOutputStream out = new DataOutputStream(System.out);
            out.write(data, 0, len);
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}

class WriteThread extends Thread
{
    public PipedOutputStream out;
    
    public WriteThread(PipedOutputStream out)
    {
        this.out = out;
    }
    
    public void connect(PipedInputStream in) throws Exception
    {
        out.connect(in);
    }
    
    public void run()
    {
        try
        {
            DataInputStream in = new DataInputStream(System.in);
            byte[] data = new byte[1024];
            int len = in.read(data, 0, 1024);
            out.write(data, 0, len);
            out.flush();
            out.close();
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}

public class Test
{
    public static void main(String args[]) throws Exception
    {
        PipedInputStream in = new PipedInputStream();
        PipedOutputStream out = new PipedOutputStream();
        
        ReadThread reader = new ReadThread(in);
        WriteThread writer = new WriteThread(out);
        
        writer.connect(in);
        
        reader.start();
        writer.start();
    }
}











阅读(722) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~