Java IO管道流

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class Test4 {

    public static void main(String[] args) {
        PipedInputStream pin = new PipedInputStream();
        PipedOutputStream pout = new PipedOutputStream();

        try {
            pin.connect(pout);// 输入流与输出流链接
        } catch (Exception e) {
            e.printStackTrace();// 这是便于调试用的,上线的时候不需要
        }

        new Thread(new ReadThread(pin)).start();
        new Thread(new WriteThread(pout)).start();

        // 输出结果:读到: 一个美女。。
        /** 可能结果
         * ReadThread读取数据,没有数据阻塞
         * WriteThread开始写入数据,等待6秒后
         * WriteThread写入内容结束
         * ReadThread读到数据,阻塞结束
         * ReadThread读到: 一个美女。。
         */
    }
}

// 读取数据的线程
class ReadThread implements Runnable {
    private PipedInputStream pin;// 输入管道

    public ReadThread(PipedInputStream pin) {
        this.pin = pin;
    }

    @Override
    public void run() {
        try {
            byte[] buf = new byte[1024];

            System.out.println("ReadThread读取数据,没有数据阻塞");
            int len = pin.read(buf);// read阻塞
            System.out.println("ReadThread读到数据,阻塞结束");

            String s = new String(buf, 0, len);
            System.out.println("ReadThread读到: " + s);
            pin.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }// run
}// ReadThread

// 写入数据的线程
class WriteThread implements Runnable {
    private PipedOutputStream pout;// 输出管道

    public WriteThread(PipedOutputStream pout) {
        this.pout = pout;
    }

    @Override
    public void run() {
        try {
            System.out.println("WriteThread开始写入数据,等待6秒后");
            Thread.sleep(6000);
            pout.write("一个美女。。".getBytes());// 管道输出流
            pout.close();
            System.out.println("WriteThread写入内容结束");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }// run
}// WriteThread

  

转载于:https://www.cnblogs.com/smartsmile/p/11618059.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java管道是一种特殊的I/O,它允许两个线程之间进行单向通信。一个线程可以将数据写入管道,另一个线程可以从管道中读取数据。Java管道通过一个管道输入和一个管道输出来实现。 以下是Java管道的基本操作: 1. 创建管道:使用Pipe类的静态方法open()创建管道,该方法返回一个Pipe对象,其中包含管道输入管道输出。 ``` Pipe pipe = Pipe.open(); ``` 2. 获取管道输入和输出:使用Pipe对象的方法获取管道输入和输出。 ``` Pipe.SourceChannel sourceChannel = pipe.source(); Pipe.SinkChannel sinkChannel = pipe.sink(); ``` 3. 向管道输出中写入数据:使用管道输出的write()方法向管道中写入数据。 ``` sinkChannel.write(ByteBuffer.wrap("Hello, World!".getBytes())); ``` 4. 从管道输入中读取数据:使用管道输入的read()方法从管道中读取数据。 ``` ByteBuffer buffer = ByteBuffer.allocate(1024); sourceChannel.read(buffer); ``` 完整的Java管道示例: ``` import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.Pipe; public class PipeExample { public static void main(String[] args) throws IOException { // 创建管道 Pipe pipe = Pipe.open(); // 获取管道输入和输出 Pipe.SourceChannel sourceChannel = pipe.source(); Pipe.SinkChannel sinkChannel = pipe.sink(); // 向管道输出中写入数据 sinkChannel.write(ByteBuffer.wrap("Hello, World!".getBytes())); // 从管道输入中读取数据 ByteBuffer buffer = ByteBuffer.allocate(1024); sourceChannel.read(buffer); System.out.println(new String(buffer.array()).trim()); } } ``` 运行结果: ``` Hello, World! ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值