You are on page 1of 13

INSTITUTE : UIE

DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
Java Programing(CST-205)
TOPIC OF PRESENTATION:

PipedWriter and PipedReader

DISCOVER . LEARN . EMPOWER


Lecture Objectives

In this lecture, we will discuss:


• PipedWriter and PipedReader

2
PipedWriter
The PipedWriter class is used to write java pipe as a stream of characters.
This class is used generally for writing text. Generally PipedWriter is connected to
a PipedReader and used by different threads.

Constructor:
Methods
PipedReader
The PipedReader class is used to read the contents of a pipe as a stream of
characters. This class is used generally to read text.
PipedReader class must be connected to the same PipedWriter and are used by
different threads.

Constructor:
Example
import java.io.PipedReader;  
import java.io.PipedWriter;  
  
public class PipeReaderExample2 {  
    public static void main(String[] args) {  
        try {  
              final PipedReader read = new PipedReader();  
            final PipedWriter write = new PipedWriter(read);  
              
Thread readerThread = new Thread(new Runnable() {  
                public void run() {  
                    try {  
                        int data = read.read();  
                        while (data != -1) {  
                            System.out.print((char) data);  
                            data = read.read();  
                        }  
                    } catch (Exception ex) {  
                    }  
                }  
            });  
 Thread writerThread = new Thread(new Runnable() {  
                public void run() {  
                    try {  
                        write.write("I love my country\n".toCharArray());  
                    } catch (Exception ex) {  
                    }  
                }  
            });  
readerThread.start();  
            writerThread.start();  
        } catch (Exception ex) {  
            System.out.println(ex.getMessage());  
        }  
      }  
}  
QUIZ:

1. PipedWriter is the sub class of

a. Writer
b. Reader
c. FileReader
d. FileInputStream

2. write(int) mtd of the PipedWriter class throws __________


Exception.
Summary:

In this session, you were able to :

•Learn about PipedWriter and PipedReader


References:
Books:
1. Balaguruswamy, Java.
2. A Primer, E.Balaguruswamy, Programming with Java, Tata McGraw Hill
Companies
3. John P. Flynt Thomson, Java Programming.

Reference Links:
https://www.geeksforgeeks.org/java-io-pipedreader-class-java/
https://www.geeksforgeeks.org/java-io-pipedwriter-class-java/
https://docs.oracle.com/javase/7/docs/api/java/io/PipedReader.html
https://docs.oracle.com/javase/7/docs/api/java/io/PipedWriter.html
https://www.javatpoint.com/java-pipedwriter-class
https://www.javatpoint.com/java-pipedreader-class
THANK YOU

You might also like