Java.io.PipedWriter.write() Method



Description

The java.io.PipedWriter.write(char[] cbuf, int off, int len) method writes len characters from the specified character array starting at offset off to this piped output stream. This method blocks until all the characters are written to the output stream. If a thread was reading data characters from the connected piped input stream, but the thread is no longer alive, then an IOException is thrown.

Declaration

Following is the declaration for java.io.PipedWriter.write() method.

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

Parameters

  • cbuf − The data.

  • off − The start offset in the data.

  • len − The number of characters to write.

Return Value

This method does not return a value.

Exception

IOException − If an I/O error occurs.

Example

The following example shows the usage of java.io.PipedWriter.write() method.

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();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

h
e
l
java_io_pipedwriter.htm
Advertisements