Java NIO(十三) Pipe(管道)
Java NIO Pipe是两个线程之间的单向数据连接。 一个管道有一个源通道和一个接收通道。 您将数据写入接收器通道。 然后可以从源通道读取这些数据。 这是一个管道原理的例子: image.png Creating a Pipe管道 通过调用Pipe.open()方法打开Pipe。 这是如何看起来如此: Pipe pipe = Pipe.open(); Writing to a Pipe 要写入管道,您需要访问接收器通道。 这是如何做到的: Pipe.SinkChannel sinkChannel = pipe.sink(); 你可以通过调用write()方法来写入SinkChannel,如下所示: String newData = "New String to write to file..." + System.currentTimeMillis(); ByteBuffer buf = ByteBuffer.allocate(48); buf.clear(); buf.put(newData.getBytes()); buf.flip(); while(buf.hasRemain...