zoukankan      html  css  js  c++  java
  • Java中管道流

    /*
    集合中与IO结合的是Properties
    IO中与多线程结合的是管道流(PipedInputStream、PipedOutputStream)
    */
    import java.io.*;
    class Read implements Runnable
    {
    	private PipedInputStream in;
    	Read(PipedInputStream in)
    	{
    		this.in = in;
    	}
    
    	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);
    			in.close();
    		}
    		catch (IOException ex)
    		{
    			throw new RuntimeException("管道读取流失败");
    		}
    	}
    }
    
    class Write implements Runnable
    {
    	private PipedOutputStream out;
    	Write(PipedOutputStream out)
    	{
    		this.out = out;
    	}
    
    	public void run()
    	{
    		try
    		{
    			out.write("piped lai la".getBytes());
    			out.close();
    		}
    		catch (IOException ex)
    		{
    			throw new RuntimeException("管道输出流失败");
    		}
    	}
    }
    
    class PipedStreamDemo
    {
    	public static void main(String[] args) throws IOException
    	{
    		PipedInputStream in = new PipedInputStream();
    		PipedOutputStream out = new PipedOutputStream();
    		in.connect(out);
    
    		Read r = new Read(in);
    		Write w = new Write(out);
    		new Thread(r).start();
    		new Thread(w).start();
    	}
    }
    

  • 相关阅读:
    Linux
    bzoj 1834
    bzoj 1002 找规律(基尔霍夫矩阵)
    bzoj 1005 组合数学 Purfer Sequence
    bzoj 1601 最小生成树
    bzoj 1001 平面图转对偶图 最短路求图最小割
    bzoj 1192 二进制
    bzoj 1012 基础线段树
    bzoj 1044 贪心二分+DP
    bzoj 1011 近似估计
  • 原文地址:https://www.cnblogs.com/dengshiwei/p/4258444.html
Copyright © 2011-2022 走看看