CharArrayWriter class methods in Java

java.io.CharArrayWriter

This class implements a character buffer that can be used as a Writer. The buffer automatically grows when data is written to the stream.

Note: The data can be retrieved using toCharArray() and toString().


Some methods of CharArrayWriter class.


append(char)This method appends the specified character to this writer.


append(CharSequence)This method appends the specified character sequence to this writer.


append(CharSequence, int, int)This method appends a subsequence of the specified character sequence to this writer.


close()This method does not release the buffer, since its contents might still be required.


flush()This method flushes the stream.


reset()This method resets the buffer so that you can use it again without throwing away the already allocated buffer.


size()This method returns the current size of the buffer.


toCharArray()This method returns a copy of the input data.


toString()This method converts input data to a string.


write(int)This method writes a character to the buffer.


write(char[], int, int)This method writes characters to the buffer.


write(String, int, int)This method writes a portion of a string to the buffer.


writeTo(Writer)This method writes the contents of the buffer to another character stream.


CharArrayWriter writeTo(Writer) in Java

writeTo(Writer): This method is available in the java.io.CharArrayWriter class of Java.

Syntax:

void java.io.CharArrayWriter.writeTo(Writer out) throws IOException

This method takes one argument. This method writes the contents of the buffer to another character stream.

Parameters: One parameter is required for this method.

out: the output stream to write to.

Returns: NA

Throws: IOException - If an I/O error occurs.

Approach

Java

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

public class CharArrayWriterwriteTo {
    public static void main(String[] args) throws IOException {
        CharArrayWriter charArrayWriter = new CharArrayWriter();

        char cbuf[] = { 'a', 'b', 'c', 'd' };
        charArrayWriter.write(cbuf);

        CharArrayWriter out = new CharArrayWriter();
        charArrayWriter.writeTo(out);

        System.out.println(out.toString());
        charArrayWriter.close();
    }
}

Output:

abcd


CharArrayWriter write(String, int, int) in Java

write(String, int, int): This method is available in the java.io.CharArrayWriter class of Java.

Syntax:

void java.io.CharArrayWriter.write(String str, int off, int len)

This method takes three arguments. This method writes a portion of a string to the buffer.

Parameters: Three parameters are required for this method.

str: String to be written from.

off: Offset from which to start reading characters.

len: Number of characters to be written.

Throws:

IndexOutOfBoundsException - If off is negative, or len is negative, or off + len is negative or greater than the length of the given string

Approach 1: When no exception

Java

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

public class CharArrayWriterwrite3 {
    public static void main(String[] args) throws IOException {
        CharArrayWriter charArrayWriter = new CharArrayWriter();

        char cbuf[] = { 'a', 'b', 'c', 'd' };
        charArrayWriter.write(cbuf);

        int off = 1, len = 3;
        String str = "HELLO";
        charArrayWriter.write(str, off, len);

        System.out.println(charArrayWriter.toString());
        charArrayWriter.close();
    }
}

Output:

abcdELL


Approach 2: IndexOutOfBoundsException 

Java

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

public class CharArrayWriterwrite3 {
    public static void main(String[] args) throws IOException {
        CharArrayWriter charArrayWriter = new CharArrayWriter();

        char cbuf[] = { 'a', 'b', 'c', 'd' };
        charArrayWriter.write(cbuf);

        int off = -1, len = 3;
        String str = "HELLO";
        charArrayWriter.write(str, off, len);

        System.out.println(charArrayWriter.toString());
        charArrayWriter.close();
    }
}

Output:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin -1, end 2, length 5 at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3734) at java.base/java.lang.String.getChars(String.java:873) at java.base/java.io.CharArrayWriter.write(CharArrayWriter.java:133)


CharArrayWriter write(char[], int, int) in Java

write(char[], int, int): This method is available in the java.io.CharArrayWriter class of Java.

Syntax:

void java.io.CharArrayWriter.write(char[] c, int off, int len)

This method takes three arguments. This method writes characters to the buffer.

Parameters: Three parameters are required for this method.

c: the data to be written.

off: the start offset in the data.

len: the number of chars that are written.

Returns: NA

Throws:

IndexOutOfBoundsException - If off is negative, or len is negative, or off + len is negative or greater than the length of the given array

Approach 1: When no exception

Java

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

public class CharArrayWriterwrite2 {
    public static void main(String[] args) throws IOException {
        CharArrayWriter charArrayWriter = new CharArrayWriter();

        char cbuf[] = { 'a', 'b', 'c', 'd' };
        charArrayWriter.write(cbuf);

        int off = 1, len = 2;
        charArrayWriter.write(cbuf, off, len);

        System.out.println(charArrayWriter.toString());
        charArrayWriter.close();
    }
}

Output:

abcdbc


Approach 2: IndexOutOfBoundsException

Java

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

public class CharArrayWriterwrite2 {
    public static void main(String[] args) throws IOException {
        CharArrayWriter charArrayWriter = new CharArrayWriter();

        char cbuf[] = { 'a', 'b', 'c', 'd' };
        charArrayWriter.write(cbuf);

        int off = -1, len = 2;
        charArrayWriter.write(cbuf, off, len);

        System.out.println(charArrayWriter.toString());
        charArrayWriter.close();
    }
}

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.io.CharArrayWriter.write(CharArrayWriter.java:102)


CharArrayWriter write(int) in Java

write(int): This method is available in the java.io.CharArrayWriter class of Java.

Syntax:

void java.io.CharArrayWriter.write(int c)

This method takes one argument. This method writes a character to the buffer.

Parameters: One parameter is required for this method.

c: int specifying a character to be written.

Returns: NA

Exceptions: NA

Approach

Java

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

public class CharArrayWriterwrite {
    public static void main(String[] args) throws IOException {
        CharArrayWriter charArrayWriter = new CharArrayWriter();

        char cbuf[] = { 'a', 'b', 'c', 'd' };
        charArrayWriter.write(cbuf);

        int c = 'M';
        charArrayWriter.write(c);

        System.out.println(charArrayWriter.toString());
        charArrayWriter.close();
    }
}

Output:

abcdM


CharArrayWriter toString() in Java

toString(): This method is available in the java.io.CharArrayWriter class of Java.

Syntax:

String java.io.CharArrayWriter.toString()

This method converts input data to a string.

Parameters: NA

Returns: the string.

Exceptions: NA

Approach

Java

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

public class CharArrayWritertoString {
    public static void main(String[] args) throws IOException {
        CharArrayWriter charArrayWriter = new CharArrayWriter();

        char cbuf[] = { 'a', 'b', 'c', 'd' };
        charArrayWriter.write(cbuf);

        System.out.println(charArrayWriter.toString());
        charArrayWriter.close();
    }
}

Output:

abcd


CharArrayWriter toCharArray() in Java

toCharArray(): This method is available in the java.io.CharArrayWriter class of Java.

Syntax:

char[] java.io.CharArrayWriter.toCharArray()

This method returns a copy of the input data.

Parameters: NA

Returns: an array of chars copied from the input data.

Exceptions: NA

Approach

Java

import java.io.CharArrayWriter;
import java.io.IOException;
import java.util.Arrays;

public class CharArrayWritertoCharArray {
    public static void main(String[] args) throws IOException {
        CharArrayWriter charArrayWriter = new CharArrayWriter();

        char cbuf[] = { 'a', 'b', 'c', 'd' };
        charArrayWriter.write(cbuf);

        System.out.println(Arrays.toString(
charArrayWriter.toCharArray()));
        charArrayWriter.close();
    }
}

Output:

[a, b, c, d]


CharArrayWriter size() in Java

size(): This method is available in the java.io.CharArrayWriter class of Java.

Syntax:

int java.io.CharArrayWriter.size()

This method returns the current size of the buffer.

Parameters: NA

Returns: an int representing the current size of the buffer.

Exceptions: NA

Approach

Java

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

public class CharArrayWritersize {
    public static void main(String[] args) throws IOException {
        CharArrayWriter charArrayWriter = new CharArrayWriter();

        char cbuf[] = { 'a', 'b', 'c', 'd' };
        charArrayWriter.write(cbuf);

        System.out.println(charArrayWriter.size());
        charArrayWriter.close();
    }
}

Output:

4


CharArrayWriter reset() in Java

reset(): This method is available in the java.io.CharArrayWriter class of Java.

Syntax:

void java.io.CharArrayWriter.reset()

This method resets the buffer so that you can use it again without throwing away the already allocated buffer.

Parameters: NA

Returns: NA

Exceptions: NA

Approach

Java

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

public class CharArrayWriterreset {
    public static void main(String[] args) throws IOException {
        CharArrayWriter charArrayWriter = new CharArrayWriter();

        char cbuf[] = { 'a', 'b', 'c', 'd' };
        charArrayWriter.write(cbuf);

        charArrayWriter.reset();

        System.out.println("Successfully reset");
        charArrayWriter.close();
    }
}

Output:

Successfully reset


CharArrayWriter flush() in Java

flush(): This method is available in the java.io.CharArrayWriter class of Java.

Syntax:

void java.io.CharArrayWriter.flush()

This method flushes the stream.

Parameters: NA

Returns: NA

Exceptions: NA

Approach

Java

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

public class CharArrayWriterflush {
    public static void main(String[] args) throws IOException {
        CharArrayWriter charArrayWriter = new CharArrayWriter();

        char cbuf[] = { 'a', 'b', 'c', 'd' };
        charArrayWriter.write(cbuf);

        charArrayWriter.flush();

        System.out.println(charArrayWriter.toString());
        charArrayWriter.close();
    }
}

Output:

abcd


CharArrayWriter close() in Java

close(): This method is available in the java.io.CharArrayWriter class of Java.

Syntax:

void java.io.CharArrayWriter.close()

This method closes the stream. This method does not release the buffer, since its contents might still be required.

Parameters: NA

Returns: NA

Exceptions: NA

Approach

Java

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

public class CharArrayWriterclose {
    public static void main(String[] args) throws IOException {
        CharArrayWriter charArrayWriter = new CharArrayWriter();

        char cbuf[] = { 'a', 'b', 'c', 'd' };
        charArrayWriter.write(cbuf);

        System.out.println(charArrayWriter.toString());
        charArrayWriter.close();
    }
}

Output:

abcd


CharArrayWriter append(CharSequence, int, int) in Java

append(CharSequence, int, int): This method is available in the java.io.CharArrayWriter class of Java.

Syntax:

CharArrayWriter java.io.CharArrayWriter.append(CharSequence csq, int start, int end)

This method takes three arguments. This method appends a subsequence of the specified character sequence to this writer.

Parameters: Three parameters are required for this method.

csq: The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null".

start: The index of the first character in the subsequence.

end: The index of the character following the last character in the subsequence.

Returns: This writer.

Throws:

IndexOutOfBoundsException - If start or end is negative, the start is greater than the end, or end is greater than csq.length().

Approach 1: When no exception

Java

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

public class CharArrayWriterappend3 {
    public static void main(String[] args) throws IOException {
        CharArrayWriter charArrayWriter = new CharArrayWriter();

        char cbuf[] = { 'a', 'b', 'c', 'd' };
        charArrayWriter.write(cbuf);

        CharSequence csq = "ABCD";
        int start = 1, end = 3;
        charArrayWriter.append(csq, start, end);

        System.out.println(charArrayWriter.toString());
        charArrayWriter.close();
    }
}

Output:

abcdBC


Approach 2: IndexOutOfBoundsException 

Java

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

public class CharArrayWriterappend3 {
    public static void main(String[] args) throws IOException {
        CharArrayWriter charArrayWriter = new CharArrayWriter();

        char cbuf[] = { 'a', 'b', 'c', 'd' };
        charArrayWriter.write(cbuf);

        CharSequence csq = "ABCD";
        int start = -1, end = 3;
        charArrayWriter.append(csq, start, end);

        System.out.println(charArrayWriter.toString());
        charArrayWriter.close();
    }
}

Output:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin -1, end 3, length 4 at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3734) at java.base/java.lang.String.substring(String.java:1903) at java.base/java.lang.String.subSequence(String.java:1942) at java.base/java.io.CharArrayWriter.append(CharArrayWriter.java:215)


CharArrayWriter append(CharSequence) in Java

append(CharSequence): This method is available in the java.io.CharArrayWriter class of Java.

Syntax:

CharArrayWriter java.io.CharArrayWriter.append(CharSequence csq)

This method takes one argument. This method appends the specified character sequence to this writer.

Parameters: One parameter is required for this method.

csq: The character sequence to append.

Note: If csq is null, then the four characters "null" are appended to this writer.

Returns: This writer.

Exceptions: NA

Approach

Java

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

public class CharArrayWriterappend2 {
    public static void main(String[] args) throws IOException {
        CharArrayWriter charArrayWriter = new CharArrayWriter();

        char cbuf[] = { 'a', 'b', 'c' };
        charArrayWriter.write(cbuf);

        CharSequence csq = "ABCD";
        charArrayWriter.append(csq);
        System.out.println(charArrayWriter.toString());
        charArrayWriter.close();
    }
}

Output:

abcABCD


CharArrayWriter append(char) in Java

append(char): This method is available in the java.io.CharArrayWriter class of Java.

Syntax:

CharArrayWriter java.io.CharArrayWriter.append(char c)

This method takes one argument. This method appends the specified character to this writer.

Parameters: One parameter is required for this method.

c: The 16-bit character to append.

Returns: This writer.

Exceptions: NA

Approach

Java

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

public class CharArrayWriterappend {
    public static void main(String[] args) throws IOException {
        CharArrayWriter charArrayWriter = new CharArrayWriter();

        char cbuf[] = { 'a', 'b', 'c' };
        charArrayWriter.write(cbuf);

        char c = 'd';
        charArrayWriter.append(c);
        System.out.println(charArrayWriter.toString());
        charArrayWriter.close();
    }
}

Output:

abcd

CharArrayReader class methods in Java

java.io.CharArrayReader

This class implements a character buffer that can be used as a character-input stream.


Some methods of CharArrayReader class.


 close()This method closes the stream and releases any system resources associated with it.


mark(int)This method marks the present position in the stream.


markSupported()This method tells whether this stream supports the mark() operation, which it does.


read()This method reads a single character.


read(char[], int, int)This method reads characters into a portion of an array.


ready()This method tells whether this stream is ready to be read.


reset(): This method resets the stream to the most recent mark, or to the beginning if it has never been marked.


skip(long)This method skips characters. Returns the number of characters that were skipped.