o7planning

Java CharArrayWriter Tutorial with Examples

  1. CharArrayWriter
  2. Examples

1. CharArrayWriter

CharArrayWriter is a subclass of Writer. True to its name, CharArrayWriter is used to write characters into a character array that it manages in the style of a Writer.
The characters written to the CharArrayWriter will be assigned to the elements of the character array that the CharArrayWriter is managing
When the number of characters written to CharArrayWriter is greater than the length of the array, CharArrayWriter will create a new array with longer length and copy the characters from the old array.
Constructors
public CharArrayWriter()  

public CharArrayWriter​(int initialSize)
  • CharArrayWriter(int) constructor creates a CharArrayWriter object with a character array that has a specified initial size.
  • CharArrayWriter() constructor creates a CharArrayWriter object with a character array of that has default size (32).
Methods of CharArrayWriter
public CharArrayWriter append​(char c)
public CharArrayWriter append​(CharSequence csq)
public CharArrayWriter append​(CharSequence csq, int start, int end)

public void close()
public void flush()
public void reset()
public int size()

public char[] toCharArray()
public String toString()

public void write​(char[] c, int off, int len)
public void write​(int c)
public void write​(String str, int off, int len)
public void writeTo​(Writer out)

2. Examples

CharArrayWriterEx1.java
package org.o7planning.chararraywriter.ex;

import java.io.CharArrayWriter;
import java.io.IOException;

public class CharArrayWriterEx1 {

    public static void main(String[] args) throws IOException {
        CharArrayWriter caw = new CharArrayWriter(1024);
        
        caw.append("One").append(" ").append("Two");
        
        caw.write(' ');
        caw.write("Three");

        char[] buffer = caw.toCharArray();
        System.out.println(buffer);
    }
}
Output:
One Two Three
For example, add 2 character arrays to create a new array.
CharArrayWriterEx2.java
package org.o7planning.chararraywriter.ex;

import java.io.CharArrayWriter;
import java.io.IOException;

public class CharArrayWriterEx2 {

    public static void main(String[] args) throws IOException {
        char[] arr1 = "Hello ".toCharArray();
        char[] arr2 = new char[] {'W', 'o', 'r', 'l', 'd', '!'};
        
        char[] result = add(arr1, arr2);
        
        System.out.println(result);
    }

    public static char[] add(char[] arr1, char[] arr2) {
        if (arr1 == null) {
            return arr2;
        }
        if (arr2 == null) {
            return arr1;
        }
        CharArrayWriter writer = new CharArrayWriter();
        try {
            writer.write(arr1);
            writer.write(arr2);
        } catch (Exception e) {
            // Never happened!
        }
        return writer.toCharArray();
    }
}
Output:
Hello World!
CharArrayWriterEx3.java
package org.o7planning.chararraywriter.ex;

import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class CharArrayWriterEx3 {

    public static void main(String[] args) throws IOException {
        try {
            String s = null;

            s.length(); // NullPointerException
        } catch (Exception e) {
            String stackTrace = getStackTrace(e);
            System.err.println(stackTrace);
        }
    }

    public static String getStackTrace(Throwable t) {
        CharArrayWriter caw = new CharArrayWriter();
        PrintWriter pw = new PrintWriter(caw);

        t.printStackTrace(pw);
        pw.close();

        return caw.toString();
    }
}
Output:
java.lang.NullPointerException
    at org.o7planning.chararraywriter.ex.CharArrayWriterEx3.main(CharArrayWriterEx3.java:13)

Java IO Tutorials

Show More