Jun 21, 2015

Java I/O - CharArrayReader and CharArrayWriter

Topics covered
1. CharArrayReader
2. CharArrayWriter
In Java FileInputStream, FileReader, FileOutputStream and FileWriter are responsible for supporting byte and character read/write operation in File.ByteArrayInputStream and ByteArrayOutputStream classes deals with byte array and act as an interface for byte array and input/output stream.

CharArrayReder and CharArrayWriter

CharArrayReader(CAR) and CharArrayWriter(CAW) provide I/O with an array of chars. CAR extends Reader and CAW extends Writer. Lets first see the internals of CAR followed by CAW and sample example for the same.
CharArrayReader(CAR) internally maintains a char buffer(char buf[]) and store array of chars that is provided to BIS constructor. Along with this buffer it also maintains two integer (pos and count) , pos keeps track of index of the next character to be read from the input char buffer and count is one greater than the position of the last char within buf. If any instance (pos == count) is true, it indicates end of array has been reached.The constructors of BIS are
//Creates a CharArrayReader from the specified array of chars - buf[].
public CharArrayReader(char buf[], int offset, int length) {
 //offset - index from where first char to read 
 //count - minimum of (offset + length, buf.length)
public CharArrayReader(char buf[]) {
Below sample program displays characters from charArrayReader and FileReader - same method deal with both type of input.
package com.devinline.io;
import java.io.BufferedReader;
import java.io.CharArrayReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class CharArrayReaderExample {

 public static void main(String[] args) throws IOException{
  int chInt = 97;
  char[] chArray = new char[10];
  for (int i = 0; i < 10; i++) {
   chArray[i] = (char) chInt++;
  Reader rd = new CharArrayReader(chArray);
  //Pass charArray
  System.out.println("Chars array output");
  //Pass File
  FileReader fr = new FileReader(new File("email.properties"));
  System.out.println("\nFile reading and displays characters");
 private static void handleReader(Reader rd){
  BufferedReader br = new BufferedReader(rd);
  int readChar;
  try {
   while((readChar = br.read())!=-1){
  } catch (IOException e) {

==========Sample output============
Char array output

File reading and displays characters
CharArrayWriter(CAW) internally maintains a buffer(char buf[]) in which chars are written. The default size of this char buffer is 32 and it automatically grows as data is written to it. Initial size of buffer is assigned 32 by constructor if no arg is passed.The constructor definition is :
public CharArrayWriter() {
 //Creates CharArrayWriter of default size 32
public CharArrayWriter(int initialSize) {
 //Exception is thrown if initialSize is < 0  
 buf = new char[initialSize];
Below is the sample program illustrating how CAW is used to write characters into chars buffer.
package com.devinline.io;

import java.io.BufferedReader;
import java.io.CharArrayWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import java.io.Reader;
import java.io.Writer;

public class CharArrayWriterExample {
 public static void main(String[] args) throws IOException{
  // created a char[] buffer of default size 32
  Writer wr = new CharArrayWriter();
  Reader ir = new FileReader(new File("email.properties"));
  BufferedReader br = new BufferedReader(ir);
  int readCount = 0;
  // Write is into ByteOutputStream
  while ((readCount = br.read()) != -1) {

  // Now verify data present in ByteOutputStream using
  // toString()/toArray() method
  System.out.println("CharArrayWriter buffer data is :  "

=========Sample output=========
CharArrayWriter buffer data is :  ID = NIK
  1. Closing CharArrayReader and CharArrayWriter has no effect. The methods of respective classes can be called even after the stream has been closed without throwing any IOException(assuming contents of buffer might still be required, buffer is not released ).
    For example:- In above sample code, after closing Writer(wr.close()), we call wr.toString() and display data present in char buffer.

Location: Hyderabad, Telangana, India