append(CharSequence, int, int): This method is available in the java.io.Writer class of Java.
Syntax:
Writer java.io.Writer.append(CharSequence csq, int start, int end) throws IOException
This method takes three arguments. This method appends a subsequence of the specified character sequence to this writer.Appendable.
Parameters: Three parameters are required for this method.
csq: The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null".
start: The index of the first character in the subsequence.
end: The index of the character following the last character in the subsequence.
Returns: This writer.
Throws:
1. IndexOutOfBoundsException - If start or end are negative, start is greater than end, or end is greater than csq.length().
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 Writerappend3 {public static void main(String[] args) throws IOException {Writer writer = new StringWriter();CharSequence csq = new String("HELLO JAVA");int start = 0, end = 5;writer.append(csq, start, end);System.out.println("Successfully append");writer.close();}}
Output:
Successfully append
Approach 2: IndexOutOfBoundsException
Java
package com.Writer;import java.io.IOException;import java.io.StringWriter;import java.io.Writer;public class Writerappend3 {public static void main(String[] args) throws IOException {Writer writer = new StringWriter();CharSequence csq = new String("HELLO JAVA");int start = 0, end = 15;writer.append(csq, start, end);System.out.println("Successfully append");writer.close();}}
Output:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 0, end 15, length 10 at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3734) at java.base/java.lang.String.substring(String.java:1903) at java.base/java.lang.String.subSequence(String.java:1942) at java.base/java.io.StringWriter.append(StringWriter.java:190) at java.base/java.io.StringWriter.append(StringWriter.java:41) at com.Writer.Writerappend3.main(Writerappend3.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