CharArrayWriter append() method in Java with Examples

The append() method of CharArrayWriter class in Java is of three types:

  1. The append(char) method of CharArrayWriter class in Java is used to append the specified character to the writer. This append() method appends one character at a time to the CharArrayWriter and returns this CharArrayWriter.
    Syntax: 
     

    public CharArrayWriter append(char c)
    
    

    Specified By: This method is specified by the append() method of Appendable interface.
    Overrides: This method overrides the append() method of Writer class.
    Parameters: This method accepts one parameter c that represents the 16-bit character that is to be appended.
    Return value: This method returns CharArrayWriter after appending the character into it.
    Exceptions: This method does not throw any exception.
    Below program illustrates append(char) method in CharArrayWriter class in IO package:
    Program: 
     

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // CharArrayWriter append(char) method
      
    import java.io.*;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // Create charArrayWriter
            CharArrayWriter charArrayWriter
                = new CharArrayWriter();
      
            // Append the character
            charArrayWriter.append('G');
      
            charArrayWriter.append('E');
      
            charArrayWriter.append('E');
      
            charArrayWriter.append('K');
      
            charArrayWriter.append('S');
      
            // print the charArrayWriter
            System.out.println(
                charArrayWriter.toString());
        }
    }

    chevron_right

    
    

    Output:

    GEEKS
    
  2. The append(CharSequence) method of CharArrayWriter class in Java is used to append the specified character sequence to the writer. This append() method appends a character sequence at a time to the CharArrayWriter and returns this CharArrayWriter.
    Syntax: 
     



    public CharArrayWriter append(CharSequence csq)
    

    Specified By: This method is specified by the append() method of Appendable interface.
    Overrides: This method overrides the append() method of Writer class.
    Parameters: This method accepts one parameter csq that represents the character sequence that is to be appended. If the character sequence is null then the 4 characters ‘null’ is appended to the CharArrayWriter.
    Return value: This method returns CharArrayWriter after appending the character sequence into it.
    Exceptions: This method does not throw any exception.
    Below program illustrates append(CharSequence) method in CharArrayWriter class in IO package:
    Program: 
     

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // CharArrayWriter append(CharSequence) method
      
    import java.io.*;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // Create charArrayWriter
            CharArrayWriter charArrayWriter
                = new CharArrayWriter();
      
            // Create character sequence
            CharSequence csq1 = "GEEKS";
      
            CharSequence csq2 = "FOR";
      
            // Append character sequences
            // to the charArrayWriter
            charArrayWriter.append(csq1);
      
            charArrayWriter.append(csq2);
      
            charArrayWriter.append(csq1);
      
            // print the charArrayWriter
            System.out.println(
                charArrayWriter.toString());
        }
    }

    chevron_right

    
    

    Output:

    GEEKSFORGEEKS
    
  3. The append(CharSequence, int, int) method of CharArrayWriter class in Java is used to append the subsequence of a specified character sequence to the writer. This append() method appends a portion of character sequence to the CharArrayWriter and returns this CharArrayWriter.
    Syntax: 
     

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

    Specified By: This method is specified by the append() method of Appendable interface.
    Overrides: This method overrides the append() method of Writer class.
    Parameters: This method accepts three parameters: 
     

    • csq – It represents the character sequence whose subsequence is to be appended.
    • start – It represents the starting index for the subsequence.
    • end – It represents the index of character following the end for the subsequence.

    Return value: This method returns CharArrayWriter after appending the subsequence of the given character sequence into it.
    Exceptions: This method throws IndexOutOfBoundsException if the start or the end are negative or start is greater than end or end is greater than the length of given character sequence.
    Below program illustrates append(CharSequence, int, int) method in CharArrayWriter class in IO package:
    Program: 
     

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // CharArrayWriter
    // append(CharSequence, int, int) method
      
    import java.io.*;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // Create charArrayWriter
            CharArrayWriter charArrayWriter
                = new CharArrayWriter();
      
            // Create character sequence
            CharSequence csq = "GEEKSFORGEEKS";
      
            // Append subsequence of character
            // sequence to the charArrayWriter
            charArrayWriter.append(csq, 8, 13);
      
            // print the charArrayWriter
            System.out.println(
                charArrayWriter.toString());
        }
    }

    chevron_right

    
    

    Output:

    GEEKS
    

References: 
1. https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#append(char) 
2. https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#append(java.lang.CharSequence) 
3. https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#append(java.lang.CharSequence, int, int)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.