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.