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

  • 相关阅读:
    维护
    zabbix监控线
    java——快排、冒泡、希尔、归并
    java——注解处理器
    spring boot——常用注解
    java——修改txt文件中某一行的内容
    spring boot——关于一个Mysql主键的问题
    mysql
    springboot
    自信点,不要怕
  • 原文地址:https://www.cnblogs.com/dengshiwei/p/4258444.html
Copyright © 2011-2022 走看看