Java - CharArrayWriter append(CharSequence cs) method



Description

The Java CharArrayWriter append(CharSequence cs) method is used to append a character sequence (like a String or StringBuilder) to the writer. This method is useful when working with character streams.

Declaration

Following is the declaration for java.io.CharArrayWriter.append(CharSequence cs) method −

public CharArrayWriter append(CharSequence cs)

Parameters

cs − The character sequence to be appended. If the character sequence is null, the writer is appended with 4 characters 'null'.

Return Value

The CharArrayWriter instance itself, allowing method chaining.

Exception

NA

Example - Using CharArrayWriter append(CharSequence cs) method

The following example shows the usage of Java CharArrayWriter append(CharSequence cs) method.

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;

public class CharArrayWriterDemo {
   public static void main(String[] args) {      
      CharArrayWriter chw = null;
      
      try {
         // create character array writer
         chw = new CharArrayWriter();
         
         // declare character sequences
         CharSequence csq = "12345 ";
         CharSequence csq1 = "67890 ";
         CharSequence csq2 = "ABCDE ";
         CharSequence csq3 = "abcde ";
         
         // append character sequences to the writer
         chw.append(csq);
         chw.append(csq1);
         chw.append(csq2);
         chw.append(csq3);
         
         // prints out the character sequences
         System.out.println(chw.toString());
         
      } catch(Exception e) {
         // for any error
         e.printStackTrace();
      } finally {
         // releases all system resources from writer
         if(chw!=null)
            chw.close();
      }
   }
}

Output

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

12345 67890 ABCDE abcde

Example - Basic Usage of append(CharSequence cs)

The following example shows the usage of Java CharArrayWriter append(CharSequence cs) method.

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;

public class CharArrayWriterDemo {
   public static void main(String[] args) {
      CharArrayWriter writer = new CharArrayWriter();

      // Append a CharSequence (String)
      writer.append("Hello");
      writer.append(" World!");

      // Convert to char array and print output
      System.out.println(writer.toString()); // Output: Hello World!
   }
}

Output

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

Hello World!

Explanation

  • A CharArrayWriter object is created.

  • The append(CharSequence cs) method appends "Hello" and " World!" to the writer.

  • The contents are printed using toString(), which converts the character array to a string.

Example - Using StringBuilder as a CharSequence

The following example shows the usage of Java CharArrayWriter append(CharSequence cs) method.

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;

public class CharArrayWriterDemo {
   public static void main(String[] args) {
      CharArrayWriter writer = new CharArrayWriter();

      // Create a StringBuilder as a CharSequence
      StringBuilder sb = new StringBuilder("Java");

      // Append StringBuilder content
      writer.append(sb);
      writer.append(" Programming");

      // Print output
      System.out.println(writer.toString()); // Output: Java Programming
   }
}

Output

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

Java Programming

Explanation

  • A StringBuilder object (sb) is used as a CharSequence.

  • The append(CharSequence cs) method appends the content of sb to the writer.

  • " Programming" is appended next.

  • The final output is "Java Programming".

Key Points

  • The append(CharSequence cs) method is useful for adding dynamic text data to CharArrayWriter.

  • It can accept any CharSequence, such as String, StringBuffer, or StringBuilder.

  • It enables method chaining since it returns this.

java_io_chararraywriter.htm
Advertisements