Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1092926
  • 博文数量: 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)

分类:

2009-07-02 17:51:06

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

class ChildThread extends Thread
{
        private PipedInputStream in;

        public ChildThread(PipedInputStream in)
        {
                this.in = in;
        }

        private void readPipe() throws Exception
        {
                byte[] data = new byte[1024];
                int len;

                while (true)
                {
                        len = in.read(data, 0, data.length);
                        if (len == -1)
                                break;
                        else
                                System.out.println(new String(data, 0, len));
                }
        }

        public void run()
        {
                try
                {
                        readPipe();
                }
                catch(Exception e)
                {
                        e.printStackTrace();
                }
        }
}

public final class tmp
{
        public static void main(String args[]) throws Exception
        {
                PipedInputStream in = new PipedInputStream();
                PipedOutputStream out = new PipedOutputStream();

                in.connect(out);

                ChildThread t = new ChildThread(in);
                t.start();

                byte[] data = new byte[1024];
                int len;

                InputStream reader = new DataInputStream(System.in);

                while (true)
                {
                        len = reader.read(data, 0, data.length);
                        if (len == -1)
                                break;
                        else
                                out.write(data, 0, len);
                }

                out.flush();
                out.close();
                in.close();
        }
}

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