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

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

Syntax:

void java.io.OutputStreamWriter.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.

off: Offset from which to start writing characters.

len: 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. IOException - If an I/O error occurs.

3. NullPointerException- If cbuf is null.

Approach 1: When no exception

Java

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

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

        OutputStream out = new FileOutputStream("D:\\hello.txt");
        OutputStreamWriter outputStreamWriter =
new OutputStreamWriter(out);

        char cbuf[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
        int off = 0, len = 4;
        outputStreamWriter.write(cbuf, off, len);
        System.out.println("Successfully writes");
        outputStreamWriter.close();
    }
}

Output:

Successfully writes


Approach 2: IndexOutOfBoundsException

Java

package com.OutputStreamWriter;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

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

        OutputStream out = new FileOutputStream("D:\\hello.txt");
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(out);

        char cbuf[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
        int off = 0, len = 14;
        outputStreamWriter.write(cbuf, off, len);
        System.out.println("Successfully writes");
        outputStreamWriter.close();
    }
}

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/sun.nio.cs.StreamEncoder.write(StreamEncoder.java:127) at java.base/java.io.OutputStreamWriter.write(OutputStreamWriter.java:208) at com.OutputStreamWriter.OutputStreamWriterwrite2.main(OutputStreamWriterwrite2.java:16)


Approach 3: IOException 

Java

package com.OutputStreamWriter;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

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

        OutputStream out = new FileOutputStream("D:\\hello.txt");
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(out);

        char cbuf[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
        int off = 0, len = 4;
        outputStreamWriter.close();
        outputStreamWriter.write(cbuf, off, len);
        System.out.println("Successfully writes");
       
    }
}

Output:

Exception in thread "main" java.io.IOException: Stream closed at java.base/sun.nio.cs.StreamEncoder.ensureOpen(StreamEncoder.java:51) at java.base/sun.nio.cs.StreamEncoder.write(StreamEncoder.java:124) at java.base/java.io.OutputStreamWriter.write(OutputStreamWriter.java:208) at com.OutputStreamWriter.OutputStreamWriterwrite2.main(OutputStreamWriterwrite2.java:17)


Approach 4: NullPointerException

Java

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

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

        OutputStream out = new FileOutputStream("D:\\hello.txt");
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(out);

        char cbuf[] = null;
        int off = 0, len = 4;
        outputStreamWriter.write(cbuf, off, len);
        System.out.println("Successfully writes");
        outputStreamWriter.close();
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "cbuf" is null at java.base/sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125) at java.base/java.io.OutputStreamWriter.write(OutputStreamWriter.java:208) at com.OutputStreamWriter.OutputStreamWriterwrite2.main(OutputStreamWriterwrite2.java:16)


Some other methods of OutputStreamWriter

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

flush()This method flushes the stream.

getEncoding()This method returns the name of the character encoding being used by this stream.

OutputStreamWriter(OutputStream)This method creates an OutputStreamWriter that uses the default character encoding.

OutputStreamWriter(OutputStream, Charset)This method creates an OutputStreamWriter that uses the given charset.

OutputStreamWriter(OutputStream, CharsetEncoder)This method creates an OutputStreamWriter that uses the given charset encoder.

OutputStreamWriter(OutputStream, String)This method creates an OutputStreamWriter that uses the named charset.

write(int)This method writes a single character.

write(char[], int, int)This method writes a portion of an array of characters.

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

No comments:

Post a Comment