This class is basically a piped character-output streams. In I/O Piped, simply means a link between two threads running in JVM at the same time. So, Pipes are used both as source or destination.
A pipe is said to be broken if a thread that was providing data bytes to the connected piped output stream is no longer alive.
Declaration:
public class PipedWriter extends Writer
Constructor :
- PipedWriter() : creates a PipedWriter, that it is not connected.
- PipedWriter(PipedReader inStream) : creates a PipedWriter, that it is connected to PipedReader – ‘inStream’.
Methods:
- write(int char) : java.io.PipedWriter.write(int char) writes specified character to the PipedWriter.
Syntax :public void write(int char) Parameters : char : character to be written Return : void Exception : -> IOException : if in case IO error occurs.
- write(char[] carray, int offset, int maxlen) : java.io.PipedWriter.write(char[] carray, int offset, int maxlen) writes maxlen character from ‘carray’ to the PipedWriter. The method blocks if no characters are left to be written to the Stream.
Syntax :public void write(char[] carray, int offset, int maxlen) Parameters : carray : data of the carray offset : starting in the destination array - 'carray'. maxlen : maximum length of array to be read Return : void Exception : -> IOException : if in case IO error occurs.
Implementation :
// Java program illustrating the working of PipedWriter// write(char[] carray, int offset, int maxlen)importjava.io.*;publicclassNewClass{publicstaticvoidmain(String[] args)throwsIOException{PipedReader geek_reader =newPipedReader();PipedWriter geek_writer =newPipedWriter();// Use of connect() : connecting geek_reader with geek_writergeek_reader.connect(geek_writer);char[] carray = {'J','A','V','A'};// Use of write(char[] carray, int offset, int maxlen)geek_writer.write(carray,0,4);inta =5;System.out.print("Use of write(carray, offset, maxlen) : ");while(a>0){System.out.print(" "+ (char) geek_reader.read());}}}chevron_rightfilter_noneOutput :
Use of write(carray, offset, maxlen) : J A V A
- close() : java.io.PipedWriter.close() closes the PipedWriter and releases the allocated resources.
Syntax :public void close() Parameters : -------------- Return : void Exception : -> IOException : if in case IO error occurs.
- connect(PipedReader destination) : java.io.PipedWriter.connect(PipedReader destination) connects the PipedWriter to the ‘destination’ PipedReader and in case ‘destination’ is pipes with some other stream, IO exception is thrown
Syntax :public void connect(PipedReader destination) Parameters : destination : the PipedReader to be connected to Return : void Exception : -> IOException : if in case IO error occurs.
- flush() : java.io.PipedWriter.flush() flushes the Output Stream.
Syntax :public void flush() Parameters : ------------ Return : void Exception : -> IOException : if in case IO error occurs.
Java code illustrating the working of PipedWriter class methods :
// Java program illustrating the working of PipedWriter// write(), connect// close(), flush()importjava.io.*;publicclassNewClass{publicstaticvoidmain(String[] args)throwsIOException{PipedReader geek_reader =newPipedReader();PipedWriter geek_writer =newPipedWriter();try{// Use of connect() : connecting geek_reader with geek_writergeek_reader.connect(geek_writer);// Use of write(int byte) :geek_writer.write(71);geek_writer.write(69);geek_writer.write(69);geek_writer.write(75);geek_writer.write(83);// Use of flush() method :geek_writer.flush();System.out.println("Use of flush() method : ");inti =5;while(i >0){System.out.print(" "+ (char) geek_reader.read());i--;}// USe of close() method :System.out.println("\nClosing the Writer stream");geek_writer.close();}catch(IOException excpt){excpt.printStackTrace();}}}chevron_rightfilter_noneOutput :
Use of flush() method : G E E K S Closing the Writer stream
Next Article: Java.io.PipedReader Class in Java
This article is contributed by Mohit Gupta 🙂. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.lang.Class class in Java | Set 1
- Java.lang.Class class in Java | Set 2
- Using predefined class name as Class or Variable name in Java
- Java.util.TimeZone Class (Set-2) | Example On TimeZone Class
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Difference between Abstract Class and Concrete Class in Java
- In Java, Can we call the main() method of a class from another class?
- Does JVM create object of Main class (the class with main())?
- Inner Class And Anonymous Inner Class that Implements Runnable | Concurrent Programming Approach 3
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.Lang.Float class in Java
- Java.io.BufferedInputStream class in Java



