Java.io - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io PipedWriter

Java.io PipedWriter

shape Introduction

The Java.io PipedWriter is the class used for writing the content of the pipe as a character stream. The piped writer class is subclass to writer class. The Java.io PipedWriter is connected with the same pipe writer but both are processed by two different threads.In case of piped writer call the close method explicitly.

Class Declaration

shape Declaration

Java.io PipedWriter class is declared below: Public class PipedWriter extends Writer

shape Fields

Java.io PipedWriter class fields are:
  • Protected Object lock : The field represents for synchronize operations this object is used.

Class Constructors

shape Table

Constructor Description
PipedWriter() The function of this constructor is to create the piped writer instance and not connected with piped reader.
PipedWriter(PipedReader snk) The function of this constructor is to create the piped writer instance which is connected to the represented piped reader.

Class Methods

shape Table

Method Description
void close () The function of this method is to close the present stream and associated to input stream systems resources are released.
void connect(pipedReader snk) The function of this method is to connect the piped writer is with piped reader .
void write(Char[] cbuf, int off, int n) By using this method it writes the data up to ‘n’ length from the output stream to an array of Characters.
void write(int c) The function of this method is to write the represented character into output stream.

Inherited Methods

shape Description

From the following classes, methods are inherited to the piped writer class.
  • Java.io.Object
  • Java.io.Writer

shape Examples

Usage of void close() method. [c]import java.io.*; public class PipedWriter { public static void main(String[] args) { // creating a new Piped writer and piped reader PipedWriter writer = new PipedWriter(); PipedReader reader = new PipedReader(); try { // connect the reader and writer writer.connect(reader); // write something writer.write(83); writer.write(80); // To close the writer System.out.println("Closing writer..........."); writer.close(); System.out.println("Writer closed."); // print what we wrote above for (int i = 0; i < 2; i++) { System.out.println("" + (char) reader.read()); } } catch (IOException ex) { ex.printStackTrace(); } } }[/c] Output Following is the result will be as follows. [c]Closing writer........... Writer closed. S P [/c] Usage of void connect(pipedReader snk) method. [c]import java.io.*; public class PipedWriter { public static void main(String[] args) { // creating a new Piped writer and piped reader PipedWriter writer = new PipedWriter(); PipedReader reader = new PipedReader(); try { // To connect the reader and writer writer.connect(reader); // write something writer.write(83); writer.write(80); writer.write(76); writer.write(69); writer.write(83); writer.write(83); writer.write(79); writer.write(78); // print what we wrote above for (int i = 0; i < 8; i++) { System.out.println("" + (char) reader.read()); } } catch (IOException ex) { ex.printStackTrace(); } } }[/c] Output Following is the result will be as follows. [c] S P L E S S O N [/c]

Summary

shape Key Points

  • The piped writer is the class used for writing the content of the pipe as a character stream.
  • The piped writer class is subclass to writer class.
  • The pipe writer is connected with the same pipe writer but both are processed by two different threads.