Java.io.PipedWriter.write() 方法



描述

java.io.PipedWriter.write(char[] cbuf, int off, int len) 方法將從指定字元陣列中從偏移量 off 開始的 len 個字元寫入此管道輸出流。此方法會阻塞,直到所有字元都寫入輸出流。如果一個執行緒正在從連線的管道輸入流讀取資料字元,但該執行緒不再存活,則會丟擲 IOException。

宣告

以下是 java.io.PipedWriter.write() 方法的宣告。

public void write(char[] cbuf, int off, int len)

引數

  • cbuf − 資料。

  • off − 資料的起始偏移量。

  • len − 要寫入的字元數。

返回值

此方法不返回值。

異常

IOException − 如果發生 I/O 錯誤。

示例

以下示例演示了 java.io.PipedWriter.write() 方法的使用。

package com.tutorialspoint;

import java.io.*;

public class PipedWriterDemo {
   public static void main(String[] args) {
      char[] c = {'h', 'e', 'l', 'l', 'o'};
      
      // create a new Piped writer and reader
      PipedWriter writer = new PipedWriter();
      PipedReader reader = new PipedReader();

      try {
         // connect the reader and the writer
         writer.connect(reader);

         // write something
         writer.write(c, 0, 3);

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

讓我們編譯並執行上述程式,這將產生以下結果:

h
e
l
java_io_pipedwriter.htm
廣告
© . All rights reserved.