Java.io.PipedOutputStream.write() 方法



描述

java.io.PipedOutputStream.write(byte[] b, int off, int len) 方法将从指定字节数组中从偏移量 off 开始的 len 个字节写入此管道输出流。此方法会阻塞,直到所有字节都写入输出流。

声明

以下是java.io.PipedOutputStream.write() 方法的声明。

public void write(byte[] b, int off, int len)

参数

  • b − 数据。

  • off − 数据中的起始偏移量。

  • len − 要写入的字节数。

返回值

此方法不返回值。

异常

IOException − 如果发生 I/O 错误。

示例

以下示例显示了java.io.PipedOutputStream.write() 方法的用法。

package com.tutorialspoint;

import java.io.*;

public class PipedOutputStreamDemo extends PipedInputStream {
   public static void main(String[] args) {
      byte[] b = {'h', 'e', 'l', 'l', 'o'};

      // create a new Piped input and Output Stream
      PipedOutputStream out = new PipedOutputStream();
      PipedInputStreamDemo in = new PipedInputStreamDemo();

      try {
         // connect input and output
         out.connect(in);

         // write something 
         out.write(b, 0, 3);

         // flush the stream
         out.flush();

         // print what we wrote
         for (int i = 0; i < 3; i++) {
            System.out.print("" + (char) in.read());
         }
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

让我们编译并运行上述程序,这将产生以下结果:

hel
java_io_pipedoutputstream.htm
广告
© . All rights reserved.