write(char[], int, int): This method is available in the java.io.PipedWriter class of Java.
Syntax:
void java.io.PipedWriter.write(char[] cbuf, int off, int len) throws IOException
This method takes three arguments. This method writes len characters from the specified character array starting at offset off to this piped output stream.
This method blocks until all the characters are written to the output stream.
If a thread was reading data characters from the connected piped input stream, but the thread is no longer alive, then an IOException is thrown.
Parameters: Three parameters are required for this method.
cbuf: the data.
off: the start offset in the data.
len: the number of characters to write.
Returns: NA
Throws:
1. IndexOutOfBoundsException - If off is negative, or len is negative, or off + len is negative or greater than the length of the given array.
2. NullPointerException - if cbuf is null.
3. IOException - if the pipe is broken, unconnected, closed or an I/O error occurs.
Approach 1: When no exception
Java
import java.io.IOException;import java.io.PipedReader;import java.io.PipedWriter;public class PipedWriterwrite2 {public static void main(String[] args) throws IOException {PipedWriter pipedWriter = new PipedWriter();PipedReader pipedReader =new PipedReader();pipedWriter.connect(pipedReader);char cbuf[] = { 'a', 'b', 'c', 'd', 'e', 'f' };int off = 0, len = 3;pipedWriter.write(cbuf, off, len);System.out.println("Successfully writes");pipedWriter.close();}}
Output:
Successfully writes
Approach 2: IndexOutOfBoundsException
Java
package com.PipedWriter;import java.io.IOException;import java.io.PipedReader;import java.io.PipedWriter;public class PipedWriterwrite2 {public static void main(String[] args) throws IOException {PipedWriter pipedWriter = new PipedWriter();PipedReader pipedReader =new PipedReader();pipedWriter.connect(pipedReader);char cbuf[] = { 'a', 'b', 'c', 'd', 'e', 'f' };int off = 0, len = 13;pipedWriter.write(cbuf, off, len);System.out.println("Successfully writes");pipedWriter.close();}}
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.io.PipedWriter.write(PipedWriter.java:154) at com.PipedWriter.PipedWriterwrite2.main(PipedWriterwrite2.java:17)
Approach 3: NullPointerException
Java
package com.PipedWriter;import java.io.IOException;import java.io.PipedReader;import java.io.PipedWriter;public class PipedWriterwrite2 {public static void main(String[] args) throws IOException {PipedWriter pipedWriter = new PipedWriter();PipedReader pipedReader = new PipedReader();pipedWriter.connect(pipedReader);char cbuf[] = null;int off = 0, len = 3;pipedWriter.write(cbuf, off, len);System.out.println("Successfully writes");pipedWriter.close();}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "cbuf" is null at java.base/java.io.PipedWriter.write(PipedWriter.java:153) at com.PipedWriter.PipedWriterwrite2.main(PipedWriterwrite2.java:17)
Approach 4: IOException
Java
package com.PipedWriter;import java.io.IOException;import java.io.PipedWriter;public class PipedWriterwrite2 {public static void main(String[] args) throws IOException {PipedWriter pipedWriter = new PipedWriter();char cbuf[] = { 'a', 'b', 'c', 'd', 'e', 'f' };int off = 0, len = 3;pipedWriter.write(cbuf, off, len);System.out.println("Successfully writes");pipedWriter.close();}}
Output:
Exception in thread "main" java.io.IOException: Pipe not connected at java.base/java.io.PipedWriter.write(PipedWriter.java:152) at com.PipedWriter.PipedWriterwrite2.main(PipedWriterwrite2.java:13)
Some other methods of PipedWriter
close(): This method closes this piped output stream and releases any system resources associated with this stream.
connect(PipedReader): This method connects this piped writer to a receiver.
flush(): This method flushes this output stream and forces any buffered output characters to be written out.
PipedWriter(): This method creates a piped writer that is not yet connected to a piped reader.
PipedWriter(PipedReader): This method creates a piped writer connected to the specified piped reader.
write(int): This method writes the specified char to the piped output stream.
write(char[], int, int): This method writes len characters from the specified character array starting at offset off to this piped output stream.
No comments:
Post a Comment