FilterWriter write(char[], int, int) in Java

write(char[], int, int): This method is available in the java.io.FilterWriter class of Java.

Syntax:

void java.io.FilterWriter.write(char[] cbuf, int off, int len) throws IOException

This method takes three arguments. This method writes a portion of an array of characters.

Parameters: Three parameters are required for this method.

cbuf: Buffer of characters to be written.

off: Offset from which to start reading characters.

len: Number of characters to be written.

Returns: NA

Throws:

1. IndexOutOfBoundsException - If the values of the off and len parameters cause the corresponding method of the underlying Writer to throw an IndexOutOfBoundsException.

2. NullPointerException - If cbuf is null.

3. IOException - If an I/O error occurs

Approach 1: When no exception

Java

import java.io.FilterWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

public class FilterWriterwrite2 {
    public static void main(String[] args) throws IOException {

        Writer writer = new StringWriter();
        FilterWriter filterWriter = new FilterWriter(writer) {
        };

        char cbuf[] = { 'a', 'b', 'c', 'c', 'd', 'f' };
        int off = 0, len = 4;
        try {
            filterWriter.write(cbuf, off, len);
            System.out.println("Successfully writes into the filter writer");
        } catch (NullPointerException e) {
            System.out.println("NullPointerException occures");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("IndexOutOfBoundsException occures");
        } catch (IOException e) {
            System.out.println("IOException occures");
        }

        filterWriter.close();
    }
}

Output:

Successfully writes into the filter writer


Approach 2: IndexOutOfBoundsException

Java

import java.io.FilterWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

public class FilterWriterwrite2 {
    public static void main(String[] args) throws IOException {

        Writer writer = new StringWriter();
        FilterWriter filterWriter = new FilterWriter(writer) {
        };

        char cbuf[] = { 'a', 'b', 'c', 'c', 'd', 'f' };
        int off = 0, len = 100;
        try {
            filterWriter.write(cbuf, off, len);
            System.out.println("Successfully writes into the filter writer");
        } catch (NullPointerException e) {
            System.out.println("NullPointerException occurs");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("IndexOutOfBoundsException occurs");
        } catch (IOException e) {
            System.out.println("IOException occurs");
        }

        filterWriter.close();
    }
}

IndexOutOfBoundsException occurs


Approach 3: NullPointerException 

Java

import java.io.FilterWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

public class FilterWriterwrite2 {
    public static void main(String[] args) throws IOException {

        Writer writer = new StringWriter();
        FilterWriter filterWriter = new FilterWriter(writer) {
        };

        char cbuf[] = null;
        int off = 0, len = 5;
        try {
            filterWriter.write(cbuf, off, len);
            System.out.println("Successfully writes into the filter writer");
        } catch (NullPointerException e) {
            System.out.println("NullPointerException occurs");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("IndexOutOfBoundsException occurs");
        } catch (IOException e) {
            System.out.println("IOException occurs");
        }

        filterWriter.close();
    }
}

NullPointerException occurs


Some other methods of FilterWriter.

close()This method closes the stream, flushing it first. 

flush()This method flushes the stream.

write(int)This method writes a single character.

write(String, int, int)This method writes a portion of a string.

No comments:

Post a Comment