Java.io - SPLessons

Java.io CharArrayWriter

Home > Lesson > Chapter 9
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io CharArrayWriter

Java.io CharArrayWriter

shape Introduction

Java.io CharArrayWriter - The character array writer is the class used for writing of data in the form of character array from the character stream. The character array writer is the class which is subclass to writer. But it doesn't use any type of resources like connection or file. The name of the class itself represents that have to write the character array from the stream.

Class Declaration

shape Declaration

Java.io CharArrayWriter class is declared as shown below: Public class CharArrayWriter extends Writer

shape Fields

Java.io CharArrayWriter class fields are:
  • Protected Object lock :The field represents for synchronize operations this object is used.
  • Protected int Count :The field represents to the index of characters in stream buffer.
  • Protected Char [] buf : The field represents to the buffer where actually the data stored.

Class Constructors

shape Table

Constructor Description
CharArrayWriter () The function of this constructor is to create the character array writer and writes in the character buffer called array.
CharArrayWriter (int size) The function of this constructor is to create the character array writer with represented size of buffer.

Class Methods

shape Table

Method Description
void close () The function of this method is to close the present stream and associated to this stream systems resources are released without any impact.
void flush () The function of this method is to flush the specified output stream.
 void write (int c) The function of this method is to write the represented character into output stream.
void write (Char[] c, int off, int n) By using this method it writes the data up to 'n' bytes from the output stream to an array of Characters.

Inherited Methods

shape Description

From the following classes, methods are inherited to the character array writer class.
  • Java.io.object
  • Java.io.Writer

shape Examples

Usage of void write(int c) method. [c]import java.io.CharArrayWriter; public class CharArrayWriter { public static void main(String[] args) { int i = 0; String str = ""; CharArrayWriter chw = null; try{ // creating a character array writer chw = new CharArrayWriter(); // To start from integer 65 to 69 for(i = 65; i<80; i++) { // To write integer to writer chw.write(i); // To get the default character set value str = chw.toString(); // To print the string System.out.println(str); } }catch(Exception e) { // If any I/O error e.printStackTrace(); }finally { // releases all type of system resources from writer if(chw!=null) chw.close(); } } }[/c] Output Following is the result will be displayed. [c]A AB ABC ABCD ABCDE ABCDEF ABCDEFG ABCDEFGH ABCDEFGHI ABCDEFGHIJ ABCDEFGHIJK ABCDEFGHIJKL ABCDEFGHIJKLM ABCDEFGHIJKLMN ABCDEFGHIJKLMNO [/c] Usage of CharArrayWriter append (Char c) method. [c]import java.io.CharArrayWriter; public class CharArrayWriter { public static void main(String[] args) { CharArrayWriter chw = null; // create buffer char[] ch = {'S','P','L','E','S','S', 'O', 'N', 'S'}; try{ // creating a new character array writer chw = new CharArrayWriter(); // For to represent each character in the buffer for(char c:ch) { // append character to the writer chw.append(c); // To print the character array as in the form string System.out.println(chw.toString()); } }catch(Exception e) { // If any exception occurs e.printStackTrace(); }finally { // To close the stream if(chw!=null) chw.close(); } } }[/c] Output Following is the result will be displayed. [c]S SP SPL SPLE SPLES SPLESS SPLESSO SPLESSON SPLESSONS [/c]

Summary

shape Key Points

  • Java.io CharArrayWriter - The character array writer is the class used for writing of data in the form of character array from the character stream.
  • The character array writer is the class which is subclass to writer.
  • But it doesn't use any type of resources like connection or file. The name of the class itself represents that have to write the character array from the stream.