Java.io.CharArrayWriter.append() Method



Description

The java.io.CharArrayWriter.append(char c) method appends the specific character to this writer.

Declaration

Following is the declaration for java.io.CharArrayWriter.append(char c) method −

public CharArrayWriter append(char c)

Parameters

c − 16-bit character to append.

Return Value

This CharArrayWriter

Exception

NA

Example

The following example shows the usage of java.io.CharArrayWriter.append(char c) method.

package com.tutorialspoint;

import java.io.CharArrayWriter;

public class CharArrayWriterDemo {
   public static void main(String[] args) {      
      CharArrayWriter chw = null;
      
      // create buffer
      char[] ch = {'A','B','C','D','E','F'};
      
      try {
         // create new character array writer
         chw = new CharArrayWriter();
         
         // for each character in the buffer
         for(char c:ch) {
            // append character to the writer
            chw.append(c);
            
            // prints the character array as string
            System.out.println(chw.toString());
         }
      } catch(Exception e) {
         // for any exception
         e.printStackTrace();
      } finally {
         // closes the stream
         if(chw!=null)
            chw.close();
      }
   }
}

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

A
AB
ABC
ABCD
ABCDE
ABCDEF
java_io_chararraywriter.htm
Advertisements