PipedReader(PipedWriter, int): This method is available in the java.io.PipedReader class of Java.
Syntax:
java.io.PipedReader.PipedReader(PipedWriter src, int pipeSize) throws IOException
This method takes two arguments. This method creates a PipedReader so that it is connected to the piped writer src and uses the specified pipe size for the pipe's buffer.
Data written to src will then be available as input from this stream.
Parameters: Two parameters are required for this method.
src: the stream to connect to.
pipeSize: the size of the pipe's buffer.
Throws:
1. IOException - if an I/O error occurs.
2. IllegalArgumentException - if pipeSize <= 0.
Approach 1: When no exception
Java
import java.io.IOException;import java.io.PipedReader;import java.io.PipedWriter;public class PipedReaderPipedReader4 {public static void main(String[] args) throws IOException {PipedWriter src = new PipedWriter();int pipeSize = 10;PipedReader pipedReader = new PipedReader(src, pipeSize);System.out.println(pipedReader);pipedReader.close();}}
Output:
java.io.PipedReader@2401f4c3
Approach 2: IllegalArgumentException
Java
package com.PipedReader;import java.io.IOException;import java.io.PipedReader;import java.io.PipedWriter;public class PipedReaderPipedReader4 {public static void main(String[] args) throws IOException {PipedWriter src = new PipedWriter();int pipeSize = 0;PipedReader pipedReader = new PipedReader(src, pipeSize);System.out.println(pipedReader);pipedReader.close();}}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: Pipe size <= 0 at java.base/java.io.PipedReader.initPipe(PipedReader.java:132) at java.base/java.io.PipedReader.<init>(PipedReader.java:98) at com.PipedReader.PipedReaderPipedReader4.main(PipedReaderPipedReader4.java:12)
Some other methods of PipedReader
close(): This method Closes this piped stream and releases any system resources associated with the stream.
connect(PipedWriter): This method causes this piped reader to be connected to the piped writer src.
PipedReader(): This method creates a PipedReader so that it is not yet connected. It must be connected to a PipedWriter before being used.
PipedReader(int): This method creates a PipedReader so that it is not yet connected and uses a specified pipe size for the pipe's buffer.
PipedReader(PipedWriter): This method creates a PipedReader so that it is connected to the piped writer src.
PipedReader(PipedWriter, int): This method creates a PipedReader so that it is connected to the piped writer src and uses the specified pipe size for the pipe's buffer.
read(): This method reads the next character of data from this piped stream.
read(char[], int, int): This method reads up to len characters of data from this piped stream into an array of characters.
ready(): This method tells whether this stream is ready to be read.
No comments:
Post a Comment