Writer write(String, int, int) in Java

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

Syntax:

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

Returns: NA

Throws:

1. IndexOutOfBoundsException - Implementations should throw this exception 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.

Approach 1: When no exception

Java

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

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

        Writer writer = new StringWriter();

        String str = "HELLO JAVA";
        int off = 0, len = 3;
        writer.write(str, off, len);

        System.out.println("Successfully writes");

        writer.close();
    }
}

Output:

Successfully writes


Approach 2: IndexOutOfBoundsException

Java

package com.Writer;

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

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

        Writer writer = new StringWriter();

        String str = "HELLO JAVA";
        int off = 0, len =13;
        writer.write(str, off, len);

        System.out.println("Successfully writes");

        writer.close();
    }
}

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.Writer.Writerwrite5.main(Writerwrite5.java:14)


Approach 3: IOException

Java

package com.Writer;

import java.io.IOException;
import java.io.PipedWriter;
import java.io.Writer;

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

        Writer writer = new PipedWriter();

        String str = "HELLO JAVA";
        int off = 0, len = 3;
        writer.write(str, off, len);

        System.out.println("Successfully writes");

        writer.close();
    }
}

Output:

Exception in thread "main" java.io.IOException: Pipe not connected at java.base/java.io.PipedWriter.write(PipedWriter.java:152) at java.base/java.io.Writer.write(Writer.java:290) at com.Writer.Writerwrite5.main(Writerwrite5.java:14)


Some other methods of Writer

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.Appendable.

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

flush()This method flushes the stream.

Writer.nullWriter()This method returns a new Writer who discards all characters

write(char[])This method writes an array of characters.

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