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.