StringWriter write(String, int, int) in Java

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

Syntax:

void java.io.StringWriter.write(String str, int off, int len)

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

Parameters: Three parameters are required for this method.

str: String to be written.

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 string

Approach 1: When no exception

Java

import java.io.IOException;
import java.io.StringWriter;

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

        StringWriter stringWriter = new StringWriter();

        String str = "HELLO JAVA";
        int off = 0, len = 3;
        stringWriter.write(str, off, len);
        System.out.println("Successfully writes");
    }
}

Output:

Successfully writes


Approach 2: IndexOutOfBoundsException

Java

package com.StringWriter;

import java.io.IOException;
import java.io.StringWriter;

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

        StringWriter stringWriter = new StringWriter();

        String str = "HELLO JAVA";
        int off = 0, len = 13;
        stringWriter.write(str, off, len);
        System.out.println("Successfully writes");
    }
}

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException: start 0, end 13, length 10 at java.base/java.lang.AbstractStringBuilder.checkRange(AbstractStringBuilder.java:1794) at java.base/java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:675) at java.base/java.lang.StringBuffer.append(StringBuffer.java:387) at java.base/java.io.StringWriter.write(StringWriter.java:122) at com.StringWriter.StringWriterwrite4.main(StringWriterwrite4.java:13)


Some other methods of StringWriter

append(char)This method appends the specified character to this writer.

append(CharSequence)This method appends the specified character sequence to this writer.

append(CharSequence, int, int)This method appends a subsequence of the specified character sequence to this writer.

close()This method closes a StringWriter.

flush()This method flushes the stream.

getBuffer()This method returns the string buffer itself.

StringWriter()This method creates a new string writer using the default initial string buffer size.

StringWriter(int)This method creates a new string writer using the specified initial string buffer size.

toString()This method returns the buffer's current value as a string.

write(int)This method writes a single character.

write(String)This method writes a string.

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