zoukankan      html  css  js  c++  java
  • java下管道流 PipedOutputStream 与PipedInputStream

    package cn.stat.p2.demo;
    
    import java.io.IOException;
    import java.io.PipedInputStream;
    import java.io.PipedOutputStream;
    
    public class pipedintpudemo {
    
        /**
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            PipedInputStream input=new PipedInputStream();
            PipedOutputStream output=new PipedOutputStream();
            //管道相联
            input.connect(output);
            new Thread(new Input(input)).start();
            new Thread(new Output(output)).start();
    
        }
    
    }
    class Input implements Runnable
    {
        private PipedInputStream in;
    
        @Override
        public void run() {
            try{
                byte[] buf=new byte[1024];
                int len=in.read(buf);
                String s=new String(buf,0,len);
                System.out.println(s);
            }catch(Exception e)
            {
                
            }
            
        }
    
        public Input(PipedInputStream in) {
            super();
            this.in = in;
        }
        
    }
    class Output implements Runnable
    {
        private PipedOutputStream out;
    
        @Override
        public void run() {
            try {
                Thread.sleep(3000);
                out.write("管道来了,哈哈".getBytes());            
            } catch (Exception e) {
                // TODO: handle exception
            }
            
        }
    
        public Output(PipedOutputStream out) {
            super();
            this.out = out;
        }
    }
  • 相关阅读:
    C语言寒假大作战04
    C语言寒假大作战03
    C语言寒假大作战02
    C语言寒假大作战01
    C语言I作业12—学期总结
    C语言I作业11
    C语言I作业10
    C语言I作业09
    C语言I作业08
    C语言寒假大作战04
  • 原文地址:https://www.cnblogs.com/zywf/p/4783878.html
Copyright © 2011-2022 走看看