Java - PipedWriter write(int c) method



Description

The Java PipedWriter write(int c) method writes the specified char to the piped 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.

The write(int c) method

  • Writes a single character (given as an int) to the connected PipedReader.

  • The value c is treated as a Unicode character.

Declaration

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

public void write(int c)

Parameters

c − The char to be written.

Return Value

This method does not return a value.

Exception

IOException − If an I/O error occurs.

Example - Usage of PipedWriter write(int c) method

The following example shows the usage of PipedWriter write(int c) method.

PipedWriterDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;

public class PipedWriterDemo {
   public static void main(String[] args) {
   
      // 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('A');
         writer.write('B');

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

Output

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

A
B

Example - Write characters one by one in a loop

The following example shows the usage of PipedWriter write(int c) method.

PipedWriterDemo.java

package com.tutorialspoint;

import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.IOException;

public class PipedWriterDemo {
   public static void main(String[] args) {
      try {
         PipedReader reader = new PipedReader();
         PipedWriter writer = new PipedWriter();
         writer.connect(reader);

         String message = "Hello";
         for (int i = 0; i < message.length(); i++) {
            writer.write(message.charAt(i));  // write one character at a time
         }
         writer.close();

         int ch;
         while ((ch = reader.read()) != -1) {
            System.out.print((char) ch);  // Output: Hello
         }

         reader.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Hello

Explanation

  • Each character of "Hello" is written individually as an int using write(int c).

  • PipedReader reads characters until the end of the stream.

Example - Writer and Reader in separate threads

The following example shows the usage of PipedWriter write(int c) method.

PipedWriterDemo.java

package com.tutorialspoint;

import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.IOException;

public class PipedWriterDemo {
   public static void main(String[] args) {
      try {
         PipedReader reader = new PipedReader();
         PipedWriter writer = new PipedWriter();
         writer.connect(reader);

         // Writer thread
         Thread writerThread = new Thread(() -> {
            try {
               String msg = "Threaded!";
               for (int i = 0; i < msg.length(); i++) {
                  writer.write(msg.charAt(i));
               }
               writer.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         });

         // Reader thread
         Thread readerThread = new Thread(() -> {
            try {
               int ch;
               while ((ch = reader.read()) != -1) {
                  System.out.print((char) ch);
               }
               reader.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         });

         writerThread.start();
         readerThread.start();

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Threaded!

Explanation

  • A producer thread writes each character as an int using write(int c).

  • A consumer thread reads and prints the characters from the connected PipedReader.

java_io_pipedwriter.htm
Advertisements