public static void main(String[] args) throws Exception { // 管道 Pipe pipe = Pipe.open(); Pipe.SinkChannel sinkChannel = pipe.sink(); // 缓冲区 ByteBuffer byteBuffer = ByteBuffer.allocate(1024); byteBuffer.put("hello pipe".getBytes()); byteBuffer.flip(); // 写入数据 sinkChannel.write(byteBuffer); Pipe.SourceChannel sourceChannel = pipe.source(); byteBuffer.flip(); // 写出数据 int length = sourceChannel.read(byteBuffer); System.err.println(new String(byteBuffer.array(), 0, length)); sourceChannel.close(); sinkChannel.close(); }