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

Java.io PipedReader

Java.io PipedReader

shape Introduction

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

Class Declaration

shape Declaration

Java.io PipedReader class is declared below: Public abstract class PipedReader extends Reader

shape Fields

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

Class Constructors

shape Table

Constructor Description
PipedReader() The function of this constructor is to create the piped reader instance.
PipedReader(int pipeSize) The function of this constructor is to create the piped reader instance with specified size of buffer or pipe.
PipedReader(PipedWriter src, int pipeSize) The function of this constructor is to create the piped reader instance with specified size of buffer or pipe with the connection to piped writer.

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(pipedWriter src) The function of this method is to connect the piped reader is with piped writer.
int read(Char[] buf, int off, int n) By using this method it reads the data up to ‘n’ bytes from the piped stream into an array of characters.
int read() The function of this method is to read  only one character.

Inherited Methods

shape Description

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

shape Examples

Usage of void close() method. [c]import java.io.*; public class PipedReader { public static void main(String[] args) { // Creating a new Piped writer and reader PipedWriter writer = new PipedWriter(); PipedReader reader = new PipedReader(); try { // Connect the reader and the writer reader.connect(writer); // To write something writer.write(83); writer.write(80); // To read what we wrote above for (int i = 0; i < 2; i++) { System.out.println("" + (char) reader.read()); } // close the reader System.out.println("Closing reader........"); reader.close(); System.out.println("Reader closed."); } catch (IOException ex) { ex.printStackTrace(); } } }[/c] Output Following is the result. [c] S P Closing reader........ Reader closed. [/c] Usage of void connect(pipedWriter src) method. [c]import java.io.*; public class PipedReader { public static void main(String[] args) { // creating a new Piped writer and reader PipedWriter writer = new PipedWriter(); PipedReader reader = new PipedReader(); try { // connect the reader with writer reader.connect(writer); // write something writer.write(83); writer.write(80); // To read 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 displayed. [c] S P[/c]

Summary

shape Key Points

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