OutputStreamWriter write(String, int, int) in Java

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

Syntax:

void java.io.OutputStreamWriter.write(String str, int off, int len) throws IOException

This method takes three arguments. This method writes a portion of a string.

Parameters: Three parameters are required for this method.

str: A String.

off: Offset from which to start writing characters.

len: Number of characters to write.

Throws:

1. IndexOutOfBoundsException - If off is negative, or len is negative, or off + len is negative or greater than the length of the given string.

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 OutputStreamWriterwrite3 {
    public static void main(String[] args) throws IOException {

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

        String str = "HELLOJAVA";
        int off = 0, len = 4;
        outputStreamWriter.write(str, 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 OutputStreamWriterwrite3 {
    public static void main(String[] args) throws IOException {

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

        String str = "HELLOJAVA";
        int off = 0, len = 14;
        outputStreamWriter.write(str, off, len);

        System.out.println("Successfully writes");
        outputStreamWriter.close();
    }
}

Output:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 0, end 14, length 9 at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3734) at java.base/java.lang.String.getChars(String.java:873) at java.base/sun.nio.cs.StreamEncoder.write(StreamEncoder.java:140) at java.base/java.io.OutputStreamWriter.write(OutputStreamWriter.java:226) at com.OutputStreamWriter.OutputStreamWriterwrite3.main(OutputStreamWriterwrite3.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 OutputStreamWriterwrite3 {
    public static void main(String[] args) throws IOException {

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

        String str = "HELLOJAVA";
        int off = 0, len = 4;
        outputStreamWriter.close();
        outputStreamWriter.write(str, 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/sun.nio.cs.StreamEncoder.write(StreamEncoder.java:141) at java.base/java.io.OutputStreamWriter.write(OutputStreamWriter.java:226) at com.OutputStreamWriter.OutputStreamWriterwrite3.main(OutputStreamWriterwrite3.java:17)


Approach 4: NullPointerException

Java

package com.OutputStreamWriter;

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

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

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

        String str = null;
        int off = 0, len = 4;
        outputStreamWriter.write(str, off, len);

        System.out.println("Successfully writes");
        outputStreamWriter.close();
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.getChars(int, int, char[], int)" because "str" is null at java.base/sun.nio.cs.StreamEncoder.write(StreamEncoder.java:140) at java.base/java.io.OutputStreamWriter.write(OutputStreamWriter.java:226) at com.OutputStreamWriter.OutputStreamWriterwrite3.main(OutputStreamWriterwrite3.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