Java - Writer append(CharSequence csq,int start,int end) method



Description

The Java Writer append(CharSequence csq,int start,int end) method appends a subsequence of the specified character sequence to this writer.

Declaration

Following is the declaration for java.io.Writer.append(CharSequence csq,int start,int end) method.

public Writer append(CharSequence csq,int start,int end)

Parameters

  • csq − The character sequence to append. If csq is null, then the four characters "null" are appended to this writer.

  • start − The index of the first character in the subsequence.

  • end − The index of the character following the last character in the subsequence.

Return Value

This method returns the current writer.

Exception

  • IndexOutOfBoundsException − If start or end are negative, start is greater than end, or end is greater than csq.length().

  • IOException − If an I/O error occurs.

Example - Usage of Writer append(CharSequence csq,int start,int end) method

The following example shows the usage of Writer append(CharSequence csq,int start,int end) method.

WriterDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

public class WriterDemo {
   public static void main(String[] args) {
      CharSequence csq = "Hello World!";

      // create a new writer
      Writer writer = new PrintWriter(System.out);

      try {
         // append a subsequence and change line
         writer.append("This is an example\n", 11, 19);

         // flush the writer
         writer.flush();

         // append a new subsequence
         writer.append(csq, 0, 5);

         // flush the writer to see the result
         writer.flush();

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

example
Hello

Example - Append Substring from a String

The following example shows the usage of Writer append(CharSequence csq,int start,int end) method.

WriterDemo.java

package com.tutorialspoint;

import java.io.StringWriter;

public class WriterDemo {
   public static void main(String[] args) {

      StringWriter writer = new StringWriter();

      CharSequence csq = "ProgrammingLanguage";
      writer.append(csq, 0, 11); // Appends "Programming"

      System.out.println("Output: " + writer.toString());

   }
}

Output

Let us compile and run the above program, this will produce the following result−

Output: Programming

Explanation

  • "ProgrammingLanguage" has characters from index 0 to 10: "Programming".

  • This range is written to the writer.

Example - Append from a StringBuilder

The following example shows the usage of Writer append(CharSequence csq,int start,int end) method.

WriterDemo.java

package com.tutorialspoint;

import java.io.StringWriter;

public class WriterDemo {
   public static void main(String[] args) {

      StringWriter writer = new StringWriter();

      StringBuilder sb = new StringBuilder("OpenAIAssistant");
      writer.append(sb, 5, 13); // Appends "IAssista"

      System.out.println("Result: " + writer.toString());

   }
}

Output

Let us compile and run the above program, this will produce the following result−

Result: IAssista

Explanation

  • sb = "OpenAIAssistant" → Characters from index 5 to 12 are "IAssista".

  • That substring is appended to the StringWriter.

java_io_writer.htm
Advertisements