• 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();
    	}
    }
    

  • 相关阅读:
    const修饰指针
    C++调用C中编译过的函数要加extern "C"
    linux常用指令(1)
    链式队列实现
    存储类别和类型限定词
    数组,指针和引用
    字符函数和字符串函数
    C/C++编译的程序占用的内存
    结构体1(嵌套使用)
    输入输出函数小结
  • 原文地址:https://www.cnblogs.com/dengshiwei/p/4258444.html
Copyright © 2020-2023  润新知